Home > Software design >  JavaScript Regexp match with negative lookahead
JavaScript Regexp match with negative lookahead

Time:09-17

I have following string:

<ul>
  <li>first</li>
</ul>

something

<ul>
  <li>second</li>
</ul>

I want to match content of both <ul> tags, but not string between first opening <ul> tag from first list and closing </ul> tag from second string. How can I achieve that? Obvious choice for me was regular expression with negative lookahead, but it seems I can't create correct regexp. I've tried this: /<ul>([\s\S] (?!<ul>))<\/ul>/gm, so "look for any character between <ul> and </ul> tags with one or more occurences, if it is not followed with <ul> characters", but it doesn't work. https://regex101.com/r/PbT1QA/1

CodePudding user response:

Greedy vs lazy capture. I just added a ?

/<ul>([\s\S] ?(?!<ul>))<\/ul>/
  • Related