Sandip Karanjekar
Sandip Karanjekar

Reputation: 850

Javascript split the string

I have string like this

"(length>10)&(length<100)"  

And i want this

(,length,>,10,),&,(,length,<,100,)

Is it possible get with javascript split and regex.

Upvotes: 1

Views: 205

Answers (3)

Bart Kiers
Bart Kiers

Reputation: 170148

Instead of a split, I'd go for a global match, which behaves more like a tokenizer:

var input = "(length>10)&(length<100)";
var tokens = input.match(/\d+|[a-zA-Z]\w*|[()]|[<>=&|]+/g);

It scans the input and matches the following patterns (in order):

\d+           # one ore more digits
|             # OR
[a-zA-Z]\w*   # an identifier
|             # OR
[()]          # a single opening- or closing parenthesis
|             # OR
[<>=&|]+      # one or more operators: '<=', '&', '|=', ...

Upvotes: 0

Florian Margaine
Florian Margaine

Reputation: 60717

"(length>10)&(length<100)".split( /([()><&])/ ).filter( Boolean )

["(", "length", ">", "10", ")", "&", "(", "length", "<", "100", ")"]

This splits at either: (, ), >, < or & (the "or" is thanks to the [] around).

Keeping the split characters is done thanks to the capture (the parentheses around the square brackets - it's ES5 though, so not supported in IE8 and below).

Finally, to remove empty strings, I use filter( Boolean ) on the array ( ES5 too, not supported in IE8 and below either).

Upvotes: 3

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

result = subject.split(/\b|(?!\w)/);

This splits at boundaries between alphanumeric and non-alphanumeric characters, additionally between two non-alnum characters. You might get an empty match at the start/end of the string, so you need to discard zero-length results.

Upvotes: 2

Related Questions