I have a string and i want to split it into array using the '|' character but not '\|':
var a = 'abc\&|\|cba';
var b = a.split(/([^\\])\|/);
b = ["abc", "&", "|cba"]
b = ["abc\&", "\|cba"]
You could use a positive lookahead for splitting.
With escaped backslash
var a = 'abc\\&|\\|cba'; var b = a.split(/\|(?=\\)/); console.log(b);
Without escaped backslash
\| matches the character
(?=\|) Positive Lookahead - Assert that the regex below can be matched
\|matches the character
Basically it looks for a pipe, and splits if another pipe is following.
var a = 'abc\&|\|cba'; var b = a.split(/\|(?=\|)/); console.log(b);