# Regular expression

1

I would like to see how I can obtain a regular expression to get the result in brackets without considering the sub brackets, I will explain with an example

``````let txt = F[aaa(0,0,0)] otra texto F[bbb(1,1,1)]
let regExp = /F\[([^\]]+)\]/g;
let matches = txt.match(regExp);
``````

For which obtain an arrangement with the following:

``````F[aaa(0,0,0)],
F[bbb(1,1,1)]
``````

I have the problem when I have the following:

``````let txt = F[aaa( F[ccc(2,2,2)],0,0)] otra texto F[bbb(1,1,1)]
``````

For which to obtain an arrangement with the following:

``````F[aaa( F[ccc(2,2,2)]  , F[bbb(1,1,1)]
``````

and what I would like to obtain would be the following:

``````F[aaa( F[ccc(2,2,2)],0,0)] y F[bbb(1,1,1)]
``````

In addition to this case I would like to consider all possible cases for example for 3 sub brackets:

``````let txt = F[aaa( F[ccc( F[ddd(3,3,3)],2,2)],0,0)] otra texto F[bbb(1,1,1)]
``````

For what I would like to get

``````F[aaa( F[ccc( F[ddd(3,3,3)],2,2)],0,0)] y F[bbb(1,1,1)]
``````

asked by LordaBUra 11.05.2018 в 22:05
source

1

At the end I solved my problem by revez I was solving from the inside out for this

``````let txt = F[aaa( F[ccc( F[ddd(3,3,3)],2,2)],0,0)] otra texto F[bbb(1,1,1)]
``````

I got

F [ddd (3,3,3)] and F [bbb (1,1,1)]

And with a recursive function I was solving outwards

Use the following expression in case someone serves you

``````/F\[([^F\[\]]+)\]/g;
``````

source
1

Can be achieved with recursive expressions, unfortunately not supported in javascript.

To do it in javascript you would have to specify it manually in the expression, but it will always be limited to the number of inner brackets that you consider when creating the expression, up to 2 inner brackets could be:

``````let regExp = /F\[(?:[^\[\]]+|\[(?:[^\[\]]+|\[[^\[\]]*\])*\])*\]/g;
``````

Or you can use the library XRegExp , although you would have to add the delimiters again if you need them since you delete them.

``````let matches = XRegExp.matchRecursive(txt, 'F\[', '\]', 'g');
let newmatches = matches.map(match => 'F[' + match + ']');
``````

If you are interested in recourse with recourse, it could be:

``````/(F*)\[(?>[^\[\]]+|(?R))*\]/g
``````