Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript Regexp match with negative lookahead

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Greedy vs lazy capture. I just added a ?

/<ul>([\s\S]+?(?!<ul>))<\/ul>/
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading