views:

660

answers:

5

I have written the following code. But it is removing only &nbsp; not <br>

var docDesc = docDescription.replace(/(&nbsp;)*/g,"");
var docDesc1 = docDescription.replace(/(<br>)*/g,"");
A: 

Try this

var text = docDescription.replace(/(?:&nbsp;|<br>)/g,'');
alex
+1  A: 

Try "\n"...see if it works.

hallie
+1  A: 

What about:

var docDesc1 = docDescription.replace(/(<br ?\/?>)*/g,"");
Matt Blaine
still it's removing   but not the <br> tag.
shaz
Has the `<br>` some content, like, e.g., a class attribute?
Boldewyn
@shaz What about the regex from the end of Boldewyn's answer, and then at the end `gi` instead of just `g`?
Matt Blaine
A: 

This will depend on the input text but I've just checked that this works:

var result = 'foo <br> bar'.replace(/(<br>)*/g, '');
alert(result);
Darin Dimitrov
+4  A: 

You can achieve removing <br> with CSS alone:

#some_element br {
  display: none;
}

If that doesn't fit your needs, and you want to really delete each <br>, it depends, if docDescription is really a string (then one of the above solutions should work, notably Matt Blaine's) or a DOM node. In the latter case, you have to loop through the br elements:

//jquery method:
$('br').remove();

Edit: Why Matt Baline's suggestion? Because he also handles the case, where the <br> appears in an XHTML context with closing slash. However, more complete would be this:

/<br[^>]*>/
Boldewyn