A1 输出 ['A','1']
A2 输出 ['A','2']
...
AAABBB666888 输出 ['AAABBB','666888']
要求不通过正则
A1 输出 ['A','1']
A2 输出 ['A','2']
...
AAABBB666888 输出 ['AAABBB','666888']
要求不通过正则
let res = []
let en = ["A","B","C",..."X","Y","Z"]
let num = [1,2,3,4,5,6,7,8,9];
遍历字符串AAABBB666888
若当前遍历的元素在en数组中,则结果放到res[0]
若当前遍历的元素在num数组中,则结果放到res[1]
var a = 'AAABBB666888ssss'
var res = [], str = '', flag = isNaN(a[0])
for(let i = 0; i < a.length; i++){
if(flag === isNaN(a[i])){
str += a[i]
}else{
res.push(str)
str = a[i]
flag = isNaN(a[i])
}
}
str && res.push(str)
console.log(res)
const splitStr = (str) => {
let index = 0;
const ans = [];
while (index < str.length) {
let cand = "";
while (index < str.length && str[index] <= "9" && str[index] >= "0") {
cand += str[index];
++index;
}
cand && ans.push(cand);
cand = "";
while (index < str.length && (str[index] > "9" || str[index] < "0")) {
cand += str[index];
++index;
}
cand && ans.push(cand);
}
return ans;
};
console.log(splitStr('AAABBB666888adsdf')); //["AAABBB", "666888", "adsdf"]
function splitByNumber(str) {
let isNumber = isNaN(str.charAt(0));
let data = [], val = "";
str.split('').forEach(v => {
if(isNaN(v) == num) return val += v;
data.push(val);
val = v
num = isNaN(v)
})
data.push(val);
return data;
}
9 回答9.4k 阅读
6 回答5.1k 阅读✓ 已解决
5 回答3.7k 阅读✓ 已解决
3 回答10.5k 阅读✓ 已解决
4 回答8k 阅读✓ 已解决
7 回答10.1k 阅读
4 回答7.4k 阅读