Splitting By " | " Not Between Square Brackets
I need to split a string, but ignoring stuff between square brackets. You can imagine this akin to ignoring stuff within quotes. I came across a solution for quotes; (\|)(?=(?:[^']
Solution 1:
This is the adapted version of the ""
regex you posted:
(\|)(?=(?:[^\]]|\[[^\]]*\])*$)
(\|)(?=(?:[^ "]| "[^ "]* ")*$) <- "" version for comparison
You replace the 2nd "
with \[
and the 1st and 3rd with \]
Solution 2:
You should better use String#match
:
s='one|two[ignore|ignore]|three';m = s.match(/[^|\[]+\[.*?\]|[^|]+/g);
//=> ["one", "two[ignore|ignore]", "three"]
Solution 3:
Yet another solution:
"one|two[ignore|ignore]|three".split( /\|(?![^\[]*\])/ )
It uses negative lookahead, unfortunately lookbehind is not supported in JS.
Solution 4:
with the split method you can use this pattern since the capturing group is displayed with results:
\|((?:[^[|]+|\[[^\]]+])*)\|
Post a Comment for "Splitting By " | " Not Between Square Brackets"