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 开发者_运维技巧
. Now I want to remove these tags from my string.
<p>this is a demo text</p>
<p>this is a demo text</p>
<p>this is a demo text</p>
<p>&nbsp; </p>
<p>&nbsp; </p>
Please help me to remove the
using regex in JavaScript.
I think you use this code
<html>
<body>
<script>
var dd = '<p>& </p>';
alert(dd);
dd = dd.replace(/( )*/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(' ') > 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 " " (encoded spaces) - empty paragraphs:
var str = "<p>this is a demo text</p>" +
"<p>this is a demo text</p>" +
"<p>&nbsp; </p>" +
"<p>this is a demo text</p>" +
"<p>&nbsp; </p>" +
"<p>&nbsp; </p>" +
"<p>this is a demo text</p>" +
"<p>this is a demo text</p>" +
"<p> &nbsp; &nbsp;&nbsp; &nbsp;</p>";
var result=str.replace(/\<p\>(?:\s|\&nbsp;)*\<\/p\>/gi,"");
精彩评论