Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - regular expression to split string on unescaped character, e.g. | but ignore \|

I read a string from file that I split on | character. For example the string is

1|test pattern|prefix|url|postfix

So split must always give me 5 substrings, which in the above case are

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

The problem comes in when any of these five substrings contains | character. I would store it as escaped \|

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

Now, you can see that string.split('|') won't give me the desired result. The desired result is

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

I have tried some regular expressions but none of these gives desired result.

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

It looks like this is only possible with negative lookbacks but I could not get one to work

like image 841
Kashif Avatar asked Sep 05 '12 11:09

Kashif


1 Answers

Another solution:

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

That said, you'll need to escape your backslash in the initial string with another backslash to make it work:

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

Working demo available here.

like image 90
sp00m Avatar answered Oct 07 '22 12:10

sp00m