Snoopy Ohoo
Snoopy Ohoo

Reputation: 109

how to globally remove "</br> " from string

i have an html string witch contain many <br/>

(with a space after >) how to globally remove them with javascript

the only way i know is
mystring = mystring.replace(/somthing/g, "somthingelse");

but i cant put <br/> in //g

Upvotes: 0

Views: 972

Answers (3)

AndrewL64
AndrewL64

Reputation: 16311

You can just split the string to remove the </br> tag and then join the string again using a character that you want to replace the </br> tag with like this:

var mystring = "Hello World</br></br>How are you doing today?</br>Once upon a time in dummy text world</br>";

mystring = mystring.split("</br>").join(""); // replace </br> with an empty string

console.log(mystring);

Upvotes: 1

Maheer Ali
Maheer Ali

Reputation: 36594

you can use '\' in your regular expression to include special character of regex like '/' here is example

const str = "<br/><br/>";
str.replace(/<\/br>/g,"else");

Upvotes: 2

nico
nico

Reputation: 2121

I don't condone the use of regex for HTML sanitization but assuming you have a legit usecase you need to escape the forward slash:

mystring = mystring.replace(/<\/br>/g, "");

Upvotes: 1

Related Questions