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:
- Regular expression string matching any number of trailing characters in R
- Remove set of characters if they are trailing characters
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.
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.