拆分字符串,包括正则表达式匹配 [英] Split string including regular expression match
问题描述
我正在用JavaScript解析一些文本.假设我有一些字符串:
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>";
请注意,每当遇到<"number">序列
我尝试用常规表达式/<\d{1,3}>/
拆分字符串,但是这样做时,我松了<"number">序列.换句话说,我最终得到"hellow world",这是一些随机文本","foo".请注意,我希望保留字符串< 1>",< 3>"和< 12>".我将如何解决这个问题?
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?
推荐答案
您需要捕获序列以保留它.
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 ) );
这篇关于拆分字符串,包括正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!