DFSOKO
DFSOKO

Reputation: 182

How to split a string on all non-alphanumeric, but also keep the delimiters?

I'm trying to split a string by all non-alphanumeric characters (apart from #) but without loosing the delimiters.

For example:

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

Should return:

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

So far I have:

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

which looks almost looks like it works fine in: http://regex101.com/r/eT1fQ9

but this give me this error in my browser:

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

Upvotes: 2

Views: 2977

Answers (3)

anubhava
anubhava

Reputation: 785631

You can use:

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", "#"]

OR using String#match:

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

Upvotes: 1

Rob W
Rob W

Reputation: 349142

You could use the string.match method, and pass a regexp with the global flag set. The return value will then be a list with all matches:

'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"]

Basically, the RegExp is constructed as follows:

(?=.)                              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

Upvotes: 3

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382304

You can do this :

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

Upvotes: 0

Related Questions