JS regex splitting

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

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

      

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

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

+3


source to share


2 answers


(.*?[^\/]),|(.+?)$

      

This will work the way you want. See demo.



http://regex101.com/r/oO8zI4/6

+1


source


In your situation, you can simply use this regex:

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

      



The idea is that the word boundary metacharacter \b,

will not match /,

because the backslash is not a word character, so there is no word boundary between /

and ,

.

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

+3


source







All Articles