Search code examples
htmlregexline-breakstrailing

Regular expression to remove HTML line breaks at end of string


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


Solution

  • 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