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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With