Split string including regular expression match

2019-08-02 09:36发布

I am parsing some text with JavaScript. Let's say I have some string:

"hello wold <1> this is some random text <3> foo <12>"

I need to place the following sub strings in an array:

myArray[0] = "hello world ";
myArray[1] = "<1>";
myArray[2] = " this is some random text ";
myArray[3] = "<3>";
myArray[4] = " foo ";
myArray[5] = "<12>";

Note that I am spliting the string whenever I encounter a <"number"> sequence

I have tried spliting the string with a regular expresion /<\d{1,3}>/ but when I do so I loose the <"number"> sequence. In other words I end up with "hellow world", " this is some random text ", " foo ". Note that I loose the strings "<1>", "<3>" and "<12>" I will like to keep that. How will I be able to solve this?

1条回答
姐就是有狂的资本
2楼-- · 2019-08-02 10:30

You need to capture the sequence to retain it.

var str = "hello wold <1> this is some random text <3> foo <12>"

str.split(/(<\d{1,3}>)/);

// ["hello wold ", "<1>", " this is some random text ", "<3>", " foo ", "<12>", ""]

In case there are issues with the capturing group in some browsers, you could do it manually like this:

var str = "hello wold <1> this is some random text <3> foo <12>",    
    re = /<\d{1,3}>/g,
    result = [],
    match,
    last_idx = 0;

while( match = re.exec( str ) ) {
   result.push( str.slice( last_idx, re.lastIndex - match[0].length ), match[0] );

   last_idx = re.lastIndex;
}
result.push( str.slice( last_idx ) );
查看更多
登录 后发表回答