开发者

Remove Tag in HTML which Contains  

I have a string which contains some HTML and I want to remove those tags whose innerHTML contains  .

For example I have a string which contains ten <p> tags and 5 of them contain 开发者_运维技巧&nbsp;. Now I want to remove these tags from my string.

&lt;p&gt;this is a demo text&lt;/p&gt;
&lt;p&gt;this is a demo text&lt;/p&gt;
&lt;p&gt;this is a demo text&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;/p&gt;

Please help me to remove the &nbsp; using regex in JavaScript.


I think you use this code

<html>
<body>
<script>
var dd = '&lt;p&gt;&amp;&nbsp; &lt;/p&gt;';
alert(dd);
dd = dd.replace(/(&nbsp)*/g,"");

alert(dd);

</script>
<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>


This will work

spo = theString;
spo = spo.replace('</p>','</p>%$%');
spo.split(spo,'%$%');
var output=[];
spo.each(function(s){ if(! s.search('&nbsp;') > 0){ output.push(s); });

Its simple if you break it down.
You know the closing p tag is the delimiter so just add a string that wont come up split it to an array. If you wanted to be super safe you can also search the original array to make sure it does not contain the injected seperator || you could just split at the closing p tag and add it back immediately search for what you do not want and push what you do to an array =)

I hope you will forgive my shorthand.


I am not sure on 100% but in my tests it works. This is remove all encoded P tags that contains only spaces or "&nbsp;" (encoded spaces) - empty paragraphs:

var str = "&lt;p&gt;this is a demo text&lt;/p&gt;" +
          "&lt;p&gt;this is a demo text&lt;/p&gt;" +
          "&lt;p&gt;&amp;nbsp; &lt;/p&gt;" +
          "&lt;p&gt;this is a demo text&lt;/p&gt;" +
          "&lt;p&gt;&amp;nbsp; &lt;/p&gt;" +
          "&lt;p&gt;&amp;nbsp; &lt;/p&gt;" +
          "&lt;p&gt;this is a demo text&lt;/p&gt;" +
          "&lt;p&gt;this is a demo text&lt;/p&gt;" +
          "&lt;p&gt;  &amp;nbsp;  &amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/p&gt;";

var result=str.replace(/\&lt;p\&gt;(?:\s|\&amp;nbsp;)*\&lt;\/p\&gt;/gi,"");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜