Home > OS >  Regular expression to remove HTML line breaks at end of string
Regular expression to remove HTML line breaks at end of string

Time:10-04

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

CodePudding user response:

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

CodePudding user response:

this should work:

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

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

  • Related