I have the following:
[list]
[*] test
[*] test
[*] test
[/list]
and I would like to create a regular expression that turns that into:
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
I know regex enough to replace simple tags, but in this case I need to replace li tags only if they are contained inside ul. Is there a way to check that before replacing?
I am using JavaScript if that matters.
Given the text:
the regex:
matches only
[*] test2,[*] test3and[*] test4. But if the[list]‘s can be nested, or a more broader set of a BB-like language needs to be parsed, I opt for a proper parser.To do the replacements, replace the regex I suggested with:
and then replace
[list]with<ul>and[/list]with</ul>(assuming[list]and[/list]are only used for lists and are not present in comments or string literals or something).When running the following snippet:
the following is printed:
A small explanation might be in order:
\[\*]\s*matches the sub string[*]followed by zero or more white space characters;([^\r\n]+)gobbles up the rest of the line and saves it in match group 1;(?=((?!\[list])[\s\S])*\[/list])ensures that every match group 1 must have a sub string[/list]ahead of without encoutering a[list]EDIT
Or better yet, do as Gumbo suggest in the comment to this answer: match all
[list] ... [/list]and then replace all[*] ...in those.