如何在所有非字母数字上拆分字符串,还要保留分隔符?

时间:2014-04-14 17:18:44

标签: javascript regex

我试图通过所有非字母数字字符(除#之外)拆分字符串,但不会丢失分隔符。

例如:

'this #string is 2b #split&like this'

应该返回:

['this ','#string ','is ','2b ','#split','&like ','this']

到目前为止,我有:

text.split(/((?=[^\w#])|(?<=[^\w#]))/g);

看起来几乎可以正常使用:http://regex101.com/r/eT1fQ9

但这会在浏览器中出现此错误:

Uncaught SyntaxError: Invalid regular expression: /((?=[^\w#])|(?<=[^\w#]))/: Invalid group 

3 个答案:

答案 0 :(得分:3)

您可以使用string.match方法,并传递一个设置了全局标志的正则表达式。然后返回值将是包含所有匹配项的列表:

'this #string is 2b #split&like this'
    .match(/(?=.)[^a-z0-9#]*[a-z0-9#]+[^a-z0-9#]*/gi)
// ["this ","#string ","is ","2b ","#split&","like ","this"]

基本上,RegExp的构造如下:

(?=.)                              To prevent empty strings
[ inverted class of delimiters ]*  To match optional leading delimiters
[ class of delimiters ]+           To match the other characters
[inverted class of delimiters]*    To match optional trailing delimiters

答案 1 :(得分:1)

您可以使用:

var text = 'this #string is 2b #split&like this #';
var arr = text.split(/((?=.)\W*(\w+\s*|$))/g).filter(Boolean);
//=> ["this ", "#string ", "is ", "2b ", "#split", "&like ", "this", "#"]

或使用String#match

var arr = text.match(/((?=.)\W*(\w+\s*|$))/g)
//=> ["this ", "#string ", "is ", "2b ", "#split", "&like ", "this", "#"]

答案 2 :(得分:0)

你可以这样做:

arr = str.match(/([^\w#]+|^)([\w#]+)/g).map(function(s){ return s.trim() });