Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove trailing html break from string?

I have a string below and I want to remove the trailing
but I'm struggling. Any help?

This is a string<br>    
next line<br>

So after my function, the string should be

This is a string<br>
next line

Doing this code below doesn't seem to be working. Well, it works but it doesn't clear out two trailing breaks.

mystring=mystring.replace(/<br>$/,''); 

So if my string is actually:

This is a string<br>
next line<br>
<br>

then the code above just returns

This is a string<br>
next line
<br>
like image 550
Mike Avatar asked Dec 10 '22 04:12

Mike


1 Answers

If you want to remove all trailing <br>s, then use a quantifier:

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

\s matches any white space characters, so even if there is line break between continuous <br>s, it will still match.

DEMO

like image 97
Felix Kling Avatar answered Dec 27 '22 22:12

Felix Kling