19

I'm trying to accomplish the same thing as seen here:

i.e. assuming you have a text like:

<p>something</p>

<!-- OPTIONAL -->

<p class="sdf"> some text</p>
<p> some other text</p>

<!-- OPTIONAL END -->

<p>The end</p>

What is the regex that would match:

<p class="sdf"> some text</p>
<p> some other text</p>

I've setup a live test here using:

<!-- OPTIONAL -->(.*?)<!-- OPTIONAL END -->

but it's not matching correctly. Also the accepted answer on the page didn't work for me. What am I missing?

Community
  • 1
  • 1
rtuner
  • 2,362
  • 3
  • 25
  • 37
  • the required `/s` modified is [not available in JavaScript](http://www.regular-expressions.info/javascript.html) – Aprillion Jun 23 '14 at 22:25
  • I fixed the formatting of your post so we can see the actual regex you used, and I disabled syntax highlighting on that part to reduce confusion. I don't know why you had backslashes before the opening angle brackets (`\<`), but they weren't doing anything useful. – Alan Moore Jun 23 '14 at 23:16

1 Answers1

36

Well unfortunately, RegExr is dependent on the JS RegExp implementation, which does not support the option to enable the flag/modifier that you need.

You are looking for the s (DotAll) modifier forcing the dot . to match newline sequences.

If you are using JavaScript, you can use this workaround:

/<!-- OPTIONAL -->([\S\s]*?)<!-- OPTIONAL END -->/
hwnd
  • 69,796
  • 4
  • 95
  • 132
  • Thanks, this seems to do the exact opposite of what I want, i.e. to remove the text in the middle. Is there a way I could reverse the regex? Great site btw. – rtuner Jun 23 '14 at 22:28
  • 3
    Just place capturing groups around what you want. `().*?()` – hwnd Jun 23 '14 at 22:36
  • 1
    How might it be possible to grab *just* what's in between? I'd like to keep the identifiers (optional and optional end). – Crhistian Ramirez Dec 20 '18 at 13:27