Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Regular Expressions split

this is my text '123,456/,789,ABC' and I want to split by ',' but not split '/,'.

var text = '123,456/,789,ABC';
var texts = text.split(/[^/],/g);
console.log(texts)

result is [ '12', '456/,78', 'ABC' ]

but I expect [ '123', '456/,789', 'ABC' ]

like image 986
Udomsak Aom Donkhampai Avatar asked Feb 13 '26 05:02

Udomsak Aom Donkhampai


1 Answers

For your situation you can simply use this regexp:

var text = '123,456/,789,ABC';
var texts = text.split(/\b,/g);
console.log(texts); // ["123", "456/,789", "ABC"]

The idea is that word boundary metacharacter \b, will not match /, because backslash is not word character, so there is no word boundary between / and ,.

RegExp test: http://regex101.com/r/qB6aT7/1

like image 143
dfsq Avatar answered Feb 15 '26 17:02

dfsq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!