JavaScript 通过正则表达式拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17051955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript split string by regex
提问by Harry
I will have a string never long than 8 characters in length, e.g.:
我将有一个长度不超过 8 个字符的字符串,例如:
// represented as array to demonstrate multiple examples
var strs = [
'11111111',
'1RBN4',
'12B5'
]
When ran through a function, I would like all digit characters to be summed to return a final string:
当运行一个函数时,我希望对所有数字字符求和以返回最终字符串:
var strsAfterFunction = [
'8',
'1RBN4',
'3B5'
]
Where you can see all of the 8 single 1
characters in the first string end up as a single 8
character string, the second string remains unchanged as at no point are there adjacent digit characters and the third string changes as the 1
and 2
characters become a 3
and the rest of the string is unchanged.
您可以看到1
第一个字符串中的所有 8 个单个字符最终都作为一个8
字符串,第二个字符串保持不变,因为没有任何相邻的数字字符,第三个字符串随着1
和2
字符变为 a3
和其余字符而变化字符串不变。
I believe the best way to do this, in pseudo-code, would be:
我相信用伪代码做到这一点的最佳方法是:
1. split the array by regex to find multiple digit characters that are adjacent
2. if an item in the split array contains digits, add them together
3. join the split array items
What would be the .split
regex to split by multiple adajcent digit characters, e.g.:
.split
由多个相邻数字字符分割的正则表达式是什么,例如:
var str = '12RB1N1'
=> ['12', 'R', 'B', '1', 'N', '1']
EDIT:
编辑:
question: What about the string "999" should the result be "27", or "9"
问题:字符串“999”的结果应该是“27”还是“9”呢?
If it was clear, always SUM the digits, 999
=> 27
, 234
=> 9
如果很清楚,总是对数字求和,999
=> 27
,234
=>9
回答by Denys Séguret
You can do this for the whole transformation :
您可以为整个转换执行此操作:
var results = strs.map(function(s){
return s.replace(/\d+/g, function(n){
return n.split('').reduce(function(s,i){ return +i+s }, 0)
})
});
For your strs
array, it returns ["8", "1RBN4", "3B5"]
.
对于您的strs
数组,它返回["8", "1RBN4", "3B5"]
.
回答by mzedeler
var results = string.match(/(\d+|\D+)/g);
Testing:
测试:
"aoueoe34243euouoe34432euooue34243".match(/(\d+|\D+)/g)
Returns
退货
["aoueoe", "34243", "euouoe", "34432", "euooue", "34243"]
回答by Shanimal
George... My answer was originally similar to dystroy's, but when I got home tonight and found your comment I couldn't pass up a challenge
乔治...我的回答原本和dystroy的类似,但是当我今晚回到家发现你的评论时,我无法放弃挑战
:)
:)
Here it is without regexp. fwiw it might be faster, it would be an interesting benchmark since the iterations are native.
这里没有正则表达式。fwiw 它可能会更快,这将是一个有趣的基准测试,因为迭代是原生的。
function p(s){
var str = "", num = 0;
s.split("").forEach(function(v){
if(!isNaN(v)){
(num = (num||0) + +v);
} else if(num!==undefined){
(str += num + v,num = undefined);
} else {
str += v;
}
});
return str+(num||"");
};
// TESTING
console.log(p("345abc567"));
// 12abc18
console.log(p("35abc2134mb1234mnbmn-135"));
// 8abc10mb10mnbmn-9
console.log(p("1 d0n't kn0w wh@t 3153 t0 thr0w @t th15 th1n6"));
// 1d0n't0kn0w0wh@t12t0thr0w0@t0th6th1n6
// EXTRY CREDIT
function fn(s){
var a = p(s);
return a === s ? a : fn(a);
}
console.log(fn("9599999gh999999999999999h999999999999345"));
// 5gh9h3
and here is the Fiddle& a new Fiddlewithout overly clever ternary