Reputation: 559
How to split using two delimiters. Below is my code where I am splitting the string using (). How to split both using () and {(
in the below string.
var str = 'abc xyz() ab{( }) pp '
var res = str.split("()");
console.log(res); //[ 'abc xyz', ' ab{( }) pp ' ]
I have tried something like this, which does not split properly
res = str.split(/[\})\()/]/
I am expecting something like below
[ 'abc xyz', ' ab', '}) pp ' ]
If str = 'abc xyz() ab{( }) pp bb yy{( kk llop'
, I want the output to be [ 'abc xyz', ' ab', '}) pp bb yy{( kk llop' ]
. The split must happen only on the first occurrence.
Upvotes: 0
Views: 83
Reputation: 45135
var str = 'abc xyz() ab{( }) pp '
var result = str.split(/(?:\(\)|\{\()/);
alert(JSON.stringify(result));
Something like:
str.split(/(?:\(\)|\{\()/)
You use |
to specify either match ()
OR {(
. The ?:
makes it non-capturing.
Upvotes: 0