摘要:例子字符串输出具体实现找到第一个数字字母出现的位置在字典中的起始位置数字结果处理数字计数标识在字典中的起始位置字母结果处理字母计数标识这种方法不好,各位要是有好的方法请告知
例子:字符串124346564abcd 输出43 65 abcd
具体实现:
str = "124346564abcd"
var num = str.match(/[0-9]/g)
var letter = str.match(/[a-z,A-Z]/g);
// let numberList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let numberList = [9,8,7,6,5,4,3,2,1,0]
let letterList = ["a", "b", "c", "d", "e"]
// 找到第一个数字(字母)出现的位置
function findFirst(arr,num) {
for (let index = 0; index < arr.length; index++) {
if (arr[index] == num) {
return index
}
}
}
let startIndex = 0
if (num.length > 0) {
// 在字典中的起始位置
startIndex = findFirst(numberList, num[0])
}
// 数字结果
let numArr = []
// 处理数字
let indexNum = 0
function main(arr) {
// 计数标识
let numIndex = 0
let result = ""
for (let index = startIndex; index < numberList.length; index++) {
if (numberList[index] == arr[indexNum]) {
numIndex ++
indexNum ++
result += numberList[index]
} else if (numIndex >= 2) {
numArr.push(result)
result = ""
startIndex = index
numIndex = 0
} else {
startIndex = findFirst(numberList, arr[indexNum])
result = ""
numIndex = 0
if (indexNum < arr.length) {
main(num)
}
break
}
}
}
let startIndexLetter = 0
if (letter.length > 0) {
// 在字典中的起始位置
startIndexLetter = findFirst(letterList, letter[0])
}
// 字母结果
let letterArr = []
// 处理字母
let indexLet = 0
function mainLetter(arr) {
// 计数标识
let numIndex = 0
let result = ""
for (let index = startIndexLetter; index < letterList.length; index++) {
if (letterList[index] == arr[indexLet]) {
numIndex ++
indexLet ++
result += letterList[index]
} else if (numIndex >= 2) {
letterArr.push(result)
result = ""
startIndexLetter = index
numIndex = 0
} else {
startIndexLetter = findFirst(letterList, arr[indexLet])
if (indexLet < arr.length) {
mainLetter(letter)
}
break
}
}
}
if (num && num.length>0) {
main(num)
console.log(numArr);
}
if (letter&&letter.length>0) {
mainLetter(letter)
console.log(letterArr);
}
这种方法不好,各位要是有好的方法请告知
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/104233.html
摘要:除法运算符总是返回浮点数。取模运算符的结果和被除数的符号正负号相同。使用位运算符时要特别注意优先级。太空船运算符组合比较符当小于等于大于时分别返回一个小于等于大于的值。递增递减运算符不影响布尔值。 一、算术运算符 1. 概览 例子 名称 结果 $a + $b 加法 $a 和 $b 的和。 $a - $b 减法 $a 和 $b 的差。 $a * $b 乘法 $a 和 ...
摘要:在的官方手册中写道支持风格的前后递增与递减运算符。第一个注意事递增递减运算符不影响布尔值。递增递减布尔值递增递减在处理字符变量的算数运算时,沿袭了的习惯,而非的。还有一个注意事项递增递减其他字符变量则无效,原字符串没有变化。 在 PHP 的官方手册中写道: PHP 支持 C 风格的前/后递增与递减运算符。 第一个注意事:递增/递减运算符不影响布尔值。递减 NULL 值也没有...
摘要:算术运算符中的算术操作主要通过算术运算符来实现,算术运算符包括一元算术运算符和二元算术运算符两种。一元算术运算符一元算术运算符用于一个单独的操作数,并产生一个新值。 算术运算符 javascript中的算术操作主要通过算术运算符来实现,算术运算符包括一元算术运算符和二元算术运算符两种。 一元算术运算符 一元算术运算符用于一个单独的操作数,并产生一个新值。在javascript中,一元运...
摘要:代码规约扫描插件以今年年初发布的阿里巴巴开发规约为标准,作为的插件形式存在,检测代码中存在不规范得位置然后给予提示。 IntelliJ IDEA是目前最好最强最智能的Java IDE,前几天,他刚刚年满18岁。 showImg(https://segmentfault.com/img/remote/1460000017974611); 本文,给大家推荐几款我私藏已久的,自己经常使用的...
摘要:与变量不同,常量不存在可变常量,如果常量名是动态的,也可以用函数来获取常量的值。执行运算符命令反引号。递增递减运算符不影响布尔值。递增递减其他字符变量则无效,原字符串没有变化。不允许用来检测常量。 变量 基本 引用赋值 global 关键字 以下结果在命令行中有效,网页php中请不要尝试。。。都是坑。Example #1 使用 global或$GLOBALS 以下为网页ph...
阅读 3898·2021-11-25 09:43
阅读 3501·2021-10-08 10:04
阅读 1914·2019-08-26 12:20
阅读 2322·2019-08-26 12:09
阅读 902·2019-08-23 18:25
阅读 3852·2019-08-23 17:54
阅读 2673·2019-08-23 17:50
阅读 1050·2019-08-23 14:33