Suppose the following line:
some text here [baz|foo] and here [foo|bar|baz] and even here [option].
I only managed to match this ugly regex ( Regex101.com demo ):
/(?:
\[
(?:
\|?
([^\|\[\]]+)
)?
(?:
\|?
([^\|\[\]]+)
)?
(?:
\|?
([^\|\[\]]+)
)?
\]
)/ugx
The thing is, I need matches to group by square brackets. So currently I have the result that I need:
[
{
"match": 1,
"children": [
{
"group": 1,
"start": 16,
"end": 19,
"value": "baz"
},
{
"group": 2,
"start": 20,
"end": 23,
"value": "foo"
}
]
},
{
"match": 2,
"children": [
{
"group": 1,
"start": 35,
"end": 38,
"value": "foo"
},
{
"group": 2,
"start": 39,
"end": 42,
"value": "bar"
},
{
"group": 3,
"start": 43,
"end": 46,
"value": "baz"
}
]
},
{
"match": 3,
"children": [
{
"group": 1,
"start": 63,
"end": 69,
"value": "option"
}
]
}
]
The result is correct, but the regular expression is limited by the number of repeating blocks in the pattern. Is there a workaround so that it matches all the parameters in square brackets?