17

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,"");
5
  • 1
    Welcome to SO, please take a few minutes to read the FAQ and the Markdown documentation (a useful synposis of which is available in the right hand margin when editing a question). Commented Mar 25, 2010 at 8:05
  • 1
    Is docDescription a simple string or had it some history (like being innerHTML() of some DOM node)? Commented Mar 25, 2010 at 8:11
  • Generally, you shouldn't parse HTML using regular expressions. Doing so on JavaScript makes even less sense; you have a parsed DOM structure, and run in context of a giant HTML parser - the browser. Commented Mar 25, 2010 at 8:19
  • @Kobi: It depends. There are situations, where you only have a serialized HTML structure (aka a string) and want to do some preprocessing before injecting it in your site (think of XSS). Commented Mar 25, 2010 at 8:20
  • Then you need a javascript html parser, same as always. I doubt this is the case though. @Shaz - I think we're missing some context here. What is docDescription? How does it look now, and how was it created? Commented Mar 25, 2010 at 8:25

8 Answers 8

39

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[^>]*>/
Sign up to request clarification or add additional context in comments.

Comments

12

Try:

var docDesc = docDescription.replace(/[&]nbsp[;]/gi," "); // removes all occurrences of &nbsp;
docDesc = docDesc.replace(/[<]br[^>]*[>]/gi,"");  // removes all <br>

Comments

3

Try this

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

Comments

2

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

Comments

2

What about:

var docDesc1 = docDescription.replace(/(<br ?\/?>)*/g,"");

3 Comments

still it's removing &nbsp; but not the <br> tag.
Has the <br> some content, like, e.g., a class attribute?
@shaz What about the regex from the end of Boldewyn's answer, and then at the end gi instead of just g?
1

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

Comments

1

You can do it like this:

var cell = document.getElementsByTagName('br');
var length = cell.length;
for(var i = 0; i < length; i++) {
    cell[0].parentNode.removeChild(cell[0]);
}

It works like a charm. No need for jQuery.

Comments

0

I using simple replace to remove &nbsp; and br tag.

JavaScript

var str = docDescription.replace(/&nbsp;/g, '').replace(/\<br\s*[\/]?>/gi, '');

jQuery

Remove br with remove() or replaceWith()

$('br').remove();

or

$('br').replaceWith(function() {
  return '';
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.