Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove &nbsp; and <br> using javascript or jQuery?

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,"");
like image 562
shaz Avatar asked Mar 25 '10 08:03

shaz


People also ask

How do you know if a tick head is still in you?

It typically looks like a small, dark-colored fleck. It may look like a splinter if it's just the tick's mouthparts. For an additional sign of a tick head still being stuck, you may also inspect the tick's body to see if it looks like pieces of the head broke off.

What do you do if a tick head is stuck in your skin?

Clean the area of the tick bite with rubbing alcohol. Using a sterilized tweezer, gently attempt to remove the tick's head with steady, strong pressure as you pull outward. If a sterilized tweezer doesn't work, you may also try to use a needle to widen the area of the tick bite to try to get the head out.

How do you remove a tick with Vaseline?

Do not try to kill, smother, or lubricate the tick with oil, alcohol, petroleum jelly, or similar material while the tick is still embedded in the skin.


2 Answers

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();

// plain JS:
var brs = common_parent_element.getElementsByTagName('br');
while (brs.length) {
  brs[0].parentNode.removeChild(brs[0]);
}

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[^>]*>/
like image 59
Boldewyn Avatar answered Oct 09 '22 14:10

Boldewyn


Try:

var docDesc = docDescription.replace(/[&]nbsp[;]/gi," "); // removes all occurrences of &nbsp;
docDesc = docDesc.replace(/[<]br[^>]*[>]/gi,"");  // removes all <br>
like image 24
Yann Saint-Dizier Avatar answered Oct 09 '22 15:10

Yann Saint-Dizier