Forum Discussion
ory_z
13 years agoContributor
For future reference of anyone finding this thread, I've added new lines instead of breaks and 2 new lines instead of paragraph end tags. I've also trimmed leading spaced.
The function now looks like this:
function Strip_HTML( 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 = aqString.Replace( text, "<br/>", "\r\n" );
text = aqString.Replace( text, "</p>", "\r\n\r\n" );
text = text.replace( stripScripts, "" );
text = text.replace( stripAllTags, " " );
text = aqString.Trim( text, 1 );
// 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 function now looks like this:
function Strip_HTML( 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 = aqString.Replace( text, "<br/>", "\r\n" );
text = aqString.Replace( text, "</p>", "\r\n\r\n" );
text = text.replace( stripScripts, "" );
text = text.replace( stripAllTags, " " );
text = aqString.Trim( text, 1 );
// 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;
}
Related Content
- 8 years ago
- 12 years ago
- 8 years ago
- 8 years ago
- 11 months ago
Recent Discussions
- 18 hours ago