Javascript字符串与正则表达式分裂 - 有趣的行为

时间:2016-07-06 21:50:51

标签: javascript regex string split

考虑以下因素:

var regex1 = /(\[delimiter(:\d*){0,1}\])/g;
var string1 = "first [delimiter] second [delimiter:2] third [delimiter:23423] fourth";

我试图提取[delimiter][delimiter:239384]或类似的实例。

按预期使用以下工作:

var array1 = string1.match(regex1);

// results in: [delimiter],[delimiter:2],[delimiter:23423]
// which is what I expected

但字符串拆分并不像我期望的那样工作:

var array2 = string1.split(regex1);

// results in: `first,[delimiter],, second ,[delimiter:2],:2, third ,[delimiter:23423],:23423, fourth`
// I'm trying to get an array: first,second,third,fourth

有人可以帮助指出这里发生了什么吗?

谢谢!

0 个答案:

没有答案