2

Say I have a html doc like so:

<!--FOO-->
  some text
<!--BAR-->

some other text

<!--FOO-->
some more text
<!--BAR-->

How can I write a javascript regex that matches both cases of

<!--FOO-->anytext<!--BAR--> 

but not the text in between ('some other text' in this case).

My regex that I thought would work is

/<!--FOO-->(.|\n)*<!--BAR-->/  

but it catches the 'some other text' as well.

Steven Harlow
  • 631
  • 2
  • 11
  • 26

1 Answers1

2

You need the non-greedy operator ?, like this:

/<!--FOO-->(.|\n)*?<!--BAR-->/

Demo

A slightly better version would be this, letting you actually capture the text between the comments:

/<!--FOO-->((?:\n|.)*?)<!--BAR-->/

Demo

That said, parsing HTML with regex rarely ends well... See here for the classic explanation of the problem. You are better off using a library, unless your parsing is limited to the very simple case in your question.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100