1

I'm trying to remove all trailing HTML line breaks from a string., e.g. <br> or <br/> etc.

So this:

<br /> remove HTML breaks after this string    <br/><br><br /><br/><br><br /><br  /><br/>

Must become this:

<br /> remove HTML breaks after this string

I checked here:

What I have so far ([<br\/>|<br>|<br \/>])?, but that matches all line breaks including spaces

Test it live: https://regex101.com/r/XuWYqx/1

Adam
  • 6,041
  • 36
  • 120
  • 208

2 Answers2

1

You can use this regex to match all trailing br tags:

\s*(?:<br\s*\/?>)+\s*$

Replace it with an empty string.

Updated RegEx Demo

RegEx Details:

  • \s*: Match 0 or more whitespaces
  • (?:<br\s*\/?>)+: Match <br followed 0 or more whitespace followed by an optional / and closing >. Repeat this match 1 or more times
  • \s*: Match 0 or more whitespaces
  • $: End of line
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

this should work:

/(<br \/> [\d\D]+)\s+<br/gmU

it puts everything up until the first <br in Group one.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Michza
  • 39
  • 3