I assume you have a literal \
in your strings, and that your question contains a typo in the input string literal. In JS C strings, you need to use a double \
to define a literal backslash (since in regular string literals, you can define escape sequences like \r
, \n
, etc).
Your regex needs to match all characters other than \
and |
or any literal \
followed with any letter. If your string can equal a literal \
, you need
var a = 'abc\\&|\\|cba';
b = a.match(/(?:[^\\|]|\\.?)+/g);
console.log(b);
The pattern matches:
(?:
- (start of a non-capturing alternation group)
[^\\|]
- any char other than \
and |
|
- or
\\.?
- a \
followed with any 1 or 0 chars but a newline
)+
- 1 or more times