Javascript is a regular expression for splitting a string into an unescaped character, for example. | but ignore \ |

I read a line from a file that I split on | character. For example, the line

1|test pattern|prefix|url|postfix

So a split should always give me 5 substrings, which in the case above

["1", "test pattern", "prefix", "url", "postfix"]

A problem occurs when any of these five substrings contains | character. I would save him as an escaped \ |

1|test pattern|prefix|url \| title |postfix

Now you can see that string.split ('|') will not give me the desired result. Desired Result:

["1", "test pattern", "prefix", "url \| title ", "postfix"]

I tried some regular expressions, but none of them give the desired result.

string.split(/[^\\]\|/)  //["", "", "prefi", "$url \| $titl", " postfix"]

It seems like this is only possible with negative callbacks, but I couldn't get it to work

+5
source share
3 answers

Another solution:

"1|test pattern|prefix|url \\| title |postfix"
.replace(/([^\\])\|/g, "$1$1|")
.split(/[^\\]\|/);

, , :

"1|test pattern|prefix|url \\| title |postfix"
                           ^

.

+6

, Javascript lookbehinds. , :

// use two backslashes in your string!
var string = '1|test pattern|prefix|url \\| title |postfix';

// create an arbitrary unique substitute character
var sub = "-";

string.replace(/\\\|/g,sub).split(/\|/);

/* replace the substituted character again in your array of strings */

- :

string.split(//\|\b//)

, .

+3

Instead of using, split()you can match all occurrences that interest you:

var rx = /([^\\\|]|\\\|?)+/gi, item, items = [];
while (item = rx.exec(str)) {
    items.push(item[0]);
}

See in action in Fiddle

+1
source

All Articles