Regex for nested BBCode
I am trying to create a regex that will capture BB codes, BB codes with optional parameters ( [url=http://]url[/url]
), etc. and work correctly with nested BB codes.
I would then recursively parse the BB codes, starting with the innermost one.
This is what I have so far, but it breaks when I try to match the nested BB code.
template:
\[(.*)\b=?([^=].*)?\](.*)\[/\1\]
Visit http://www.gskinner.com/RegExr/ and try the template and text below
Try the following:
[b]sdfsdf[/b]
[b=extra]sdfsdf[/b]
[b=extra]left[u]middle[/u]right[/b]
+2
source to share
1 answer
You cannot use regular expressions to create a parser that supports nesting; you will need to parse the string yourself with a state machine.
Alternatively, reuse existing code .
+3
source to share