开发者

group parts of a string into an array element

开发者 https://www.devze.com 2023-03-12 09:50 出处:网络
If I have a string... abcdefghi and I want to use regex to load every elemnent into an array but I want to be able to stick anything connected by plus sign into开发者_如何学运维 the same element... ho

If I have a string... abcdefghi and I want to use regex to load every elemnent into an array but I want to be able to stick anything connected by plus sign into开发者_如何学运维 the same element... how to do that?

var mystring = "abc+d+efghi"

output array ["a","b","cde","f","g","h","i"]


One way to do it:

var re = /([^+])(?:\+[^+])*/g;
var str = 'abcd+e+fghi';
var a = str.match(re).map(function (s) { return s.replace(/\+/g, ''); });
console.log(a);

The value of a[3] should now be 'def'. http://jsfiddle.net/rbFwR/2


You can use this expression, to produce [a][b][c+d+e][f][g][h][i].

mystring.split ("(.\+)*.")

Next, replace any + characters with empty on the resulting list.


mystring.split("\\+")

Click here for more information.

0

精彩评论

暂无评论...
验证码 换一张
取 消