0

I know this have been similarly asked multiple times, but regex is somehow tricky and I can't find my particular need on other answers.

I have this html string:

... other content<div><br></div> <div>End of useful html string</div> <div><br></div> <div><br></div> <div><br></div>

and this regex pattern:

/<div><br><\/div>/gi

It matches all <div><br><\/div>. Really simple up to that point.

But the thing is that I would like to remove only that pattern after the end of the useful html string, in other words, when the pattern starts to be only <div><br></div> <div><br></div> <div><br></div> and nothing before <div>End of useful html string</div>

Just to clarify, there maybe lots of additional <div><br></div> at the end.

This is to remove the new lines at the end of contentEditable div.

electronixG
  • 136
  • 1
  • 1
  • 18
  • 5
    [Don't use Regex to parse HTML](https://stackoverflow.com/a/1732454/519413). Use a HTML parser and remove the nodes directly. – Rory McCrossan Oct 08 '22 at 18:37

1 Answers1

0

It's quite hard to understand your question but.

This will match every <div><br><\/div> after <div>End of string</div>

/(?<=<div>End of string<\/div>.+)<div><br><\/div>/

And this will match only the last one in the string:

/<div><br><\/div>$/
Konrad
  • 21,590
  • 4
  • 28
  • 64