Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regular expression to remove unwanted <br>, &nbsp;

I have a JS stirng like this
&lt;div id="grouplogo_nav"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ul&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;li&gt;&lt;a class="group_hlfppt" target="_blank" href="http://www.hlfppt.org/"&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/ul&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/div&gt;

I need to remove all <br> and $nbsp; that are only between &gt; and &lt;. I tried to write a regular expression, but didn't got it right. Does anybody have a solution.

EDIT :

Please note i want to remove only the tags b/w &gt; and &lt;

like image 643
Nandakumar V Avatar asked Oct 11 '12 11:10

Nandakumar V


People also ask

How to remove line breaks in js?

Remove All Line Breaks from a String We can remove all line breaks by using a regex to match all the line breaks by writing: str = str. replace(/(\r\n|\n|\r)/gm, ""); \r\n is the CRLF line break used by Windows.

How to remove empty spaces in js?

JavaScript String trim() The trim() method removes whitespace from both sides of a string.

What is \d in JavaScript regex?

The RegExp \D Metacharacter in JavaScript is used to search non digit characters i.e all the characters except digits. It is same as [^0-9].


2 Answers

Avoid using regex on html!

Try creating a temporary div from the string, and using the DOM to remove any br tags from it. This is much more robust than parsing html with regex, which can be harmful to your health:

var tempDiv = document.createElement('div');
tempDiv.innerHTML = mystringwithBRin;
var nodes = tempDiv.childNodes;
for(var nodeId=nodes.length-1; nodeId >= 0; --nodeId) {
    if(nodes[nodeId].tagName === 'br') {
        tempDiv.removeChild(nodes[nodeId]);
    }
}
var newStr = tempDiv.innerHTML;

Note that we iterate in reverse over the child nodes so that the node IDs remain valid after removing a given child node.

http://jsfiddle.net/fxfrt/

like image 162
Phil H Avatar answered Sep 23 '22 09:09

Phil H


myString = myString.replace(/^(&nbsp;|<br>)+/, '');

... where /.../ denotes a regular expression, ^ denotes start of string, ($nbsp;|<br>) denotes "&nbsp; or <br>", and + denotes "one or more occurrence of the previous expression". And then simply replace that full match with an empty string.

like image 35
dsgriffin Avatar answered Sep 20 '22 09:09

dsgriffin