3

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(/([^\\])\|/);

result :

b = ["abc", "&", "|cba"]

expected output :

b = ["abc\&", "\|cba"]

Basically I cannot use capturing groups in .split() function properly.

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
kingshuk basak
  • 423
  • 2
  • 8
  • 3
    Just to clarify: you have `var a = 'abc\\&|\\|cba';` (=`abc\&|\|cba`) or really `var a = 'abc\&|\|cba';` (=`abc&||cba`)? Also, you do not need a capturing group here, you need a lookbehind, but since it is missing in JS regex flavor, you need to *match* the strings you require. – Wiktor Stribiżew Jul 25 '16 at 07:59
  • Try [`b = a.match(/(?:[^\\|]|\\.)+/g)`](https://jsfiddle.net/r97opth9/) – Wiktor Stribiżew Jul 25 '16 at 08:06
  • If you need to separate the extracted words using "|" do you intentionally need only the split words beginning with alphabets like `["abc\&", "cba"]`? or else your array will be split as follows according to your string variable a -> `["abc\&", "\", "cba"]` – Nayantara Jeyaraj Jul 25 '16 at 08:07
  • @WiktorStribiżew - i have 'abc\&|\|cba that is single escaping. – kingshuk basak Jul 25 '16 at 08:54

3 Answers3

1

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 | literally

  • (?=\|) Positive Lookahead - Assert that the regex below can be matched

    • \| matches the character | literally

Basically it looks for a pipe, and splits if another pipe is following.

var a = 'abc\&|\|cba';
var b = a.split(/\|(?=\|)/);
console.log(b);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can do it as follows using regex expressions, storing each identified word( in this case the price) in an array and then grabbing it when needed

var re = /(?:^|[ ])|([a-zA-Z]+)/gm;
var str = 'abc\&|\|cba';
var identifiedWords;

while ((identifiedWords = re.exec(str)) != null) 
{
    if (identifiedWords.index === re.lastIndex) 
    {
        re.lastIndex++;
    }
// View your result using the "identifiedWords" variable.
// eg identifiedWords[0] = abc\&
// identifiedWords[1] = cba

}

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
0

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563