I want to delete empty tags such as <label></label>
, <font> </font>
so that:
JavaScript
x
5
1
<label></label><form></form>
2
<p>This is <span style="color: red;">red</span>
3
<i>italic</i>
4
</p>
5
will be cleaned as:
JavaScript
1
4
1
<p>This is <span style="color: red;">red</span>
2
<i>italic</i>
3
</p>
4
I have this RegEx in javascript, but it deletes the the empty tags but it also delete this: "<i>italic</i></p>"
JavaScript
1
2
1
str=str.replace(/<[S]+></[S]+>/gim, "");
2
What I am missing?
Advertisement
Answer
You have “not spaces” as your character class, which means “<i>italic</i></p>
” will match. The first half of your regex will match “<(i>italic</i)>
” and the second half “</(p)>
“. (I’ve used brackets to show what each [S]+
matches.)
Change this:
JavaScript
1
2
1
/<[S]+></[S]+>/
2
To this:
JavaScript
1
2
1
/<[^/>][^>]*></[^>]+>/
2
Overall you should really be using a proper HTML processor, but if you’re munging HTML soup this should suffice 🙂