Forum Discussion
AlexeyKolosov
14 years agoStaff
Hi Ory,
Here is the corrected version:
function stripHtml(html)
{
var stripStyles = new RegExp("(<style[^>]*?>(.+?)(</style[^>]*?>|$))", "g");
var stripScripts = new RegExp("(<script[^>]*?>(.+?)(</script[^>]*?>|$))", "g");
var stripAllTags = new RegExp("(<[^<]*?>)", "g");
var text = html.replace(stripStyles, "");
text = text.replace(stripScripts, "");
text = text.replace(stripAllTags, " ");
// Expand common entities
var entityMapping = {};
entityMapping["""] = "\"";
entityMapping["'"] = "'";
entityMapping["<"] = "<";
entityMapping[">"] = ">";
entityMapping[" "] = " ";
entityMapping["&"] = "&";
for (var key in entityMapping)
{
text = text.replace(new RegExp(key, "g"), entityMapping[key]);
}
// Remove duplicated spaces
text = text.replace(new RegExp("(\\s){2,}", "g"), " ");
return text;
}
The original RegExp was correct, it was just necessary to split it into multiple sub-expressions, which should be executed one by one.
Related Content
- 8 years ago
- 12 years ago
- 8 years ago
- 8 years ago
- 11 months ago
Recent Discussions
- 18 hours ago