Thanks. Close. Tried it and the SPAN tags remain...
Main Topics
Browse All TopicsHi all,
I need a javascript function that will accept a string of up to 8000 characters, and remove all HTML tags except the following:
<EM></EM>
<STRONG></STRONG>
<U></U>
<em></em>
<strong></strong>
<u></u>
Optionally, the function should also strip attributes from any <P></P> tags, leaving behind the <P> tags without attributes. Example:
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 20pt"><U>Test</U></SPAN><F
Becomes:
<P><U>Test</U> Hello. This is the <STRONG>example.</STRONG><
I'll increase points for a working solution that includes the <p> attributes portion.
Thanks,
SquareHead
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Yes, my second regexp was errorous.
/..|p|../i also matches span. Should have been /^..|p|..$/i
<html>
<head>
<script type="text/javascript">
function stripHtml(str) {
var newstr = str;
var regExp = /<\/?(\w+)(.*?)>/ig;
i = 0;
while(i++ < 10 && (mt = regExp.exec(str))) {
oldstr = mt[0];
tag = mt[1];
pars = mt[2];
if(tag.match(/^(em|strong|
repl = oldstr.replace(pars,'');
} else {
repl = '';
}
newstr = newstr.replace(oldstr, repl, "g");
}
regexp = null;
return newstr;
}
</script>
</head>
<body>
<form>
<p>
<b>In</b>
<textarea name="myIn" rows="10" cols="60"><P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 20pt"><U>Test</U></SPAN><F
<br/><input type="button" value="go" onclick="this.form.myText.
</p>
<p>
<b>Out</b>
<textarea name="myText" rows="10" cols="60"></textarea>
</p>
</form>
</html>
-r-
Example:
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" bla="sdjhhs f dhjfs j sdfhksj"><SPAN style="FONT-SIZE: 20pt"><U>Test</U></SPAN><F
Result:
<P><U>Test</U> Hello. This is the <STRONG>example.</STRONG><
Business Accounts
Answer for Membership
by: RoonaanPosted on 2006-04-17 at 10:43:58ID: 16470879
Here you go:
p/i)) { ONT size=3> Hello. This is the </FONT><SPAN style="FONT-SIZE: 8pt"><STRONG>example.</STR ONG></SPAN ></P></tex tarea> value=stri pHtml(this .form.myIn .value);" />
<html>
<head>
<script type="text/javascript">
function stripHtml(str) {
var newstr = str;
var regExp = /<\/?(\w+)(.*?)>/ig;
i = 0;
while(i++ < 10 && (mt = regExp.exec(str))) {
oldstr = mt[0];
tag = mt[1];
pars = mt[2];
if(tag.match(/em|strong|u|
repl = oldstr.replace(pars,'');
} else {
repl = '';
}
newstr = newstr.replace(oldstr, repl);
}
return newstr;
}
</script>
</head>
<body>
<form>
<p>
<b>In</b>
<textarea name="myIn" rows="10" cols="60"><P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 20pt"><U>Test</U></SPAN><F
<br/><input type="button" value="go" onclick="this.form.myText.
</p>
<p>
<b>Out</b>
<textarea name="myText" rows="10" cols="60"></textarea>
</p>
</form>
</html>
-r-