What is JavaScript’s Equivalent to PHP strip_tags()
A couple of days ago I was working on a project using jQuery where I was traversing through a table and totaling several numeric values. The problem I ran into was that most of these values were wrapped in HTML tags and the tags were not standardized from field to field so a simple replace() was not sufficient.
Unfortunately, much to my dismay jQuery does not have a built in function to strip HTML from a string like the handy strip_tags() PHP offers. I did some searching online and was able to track down a couple functions written by other developers that were able to do this task. I have taken my two favorites and combined them into the solution provided below.
/*************************************************** STRIP HTML TAGS ****************************************************/ function strip_tags(html){ //PROCESS STRING if(arguments.length < 3) { html=html.replace(/<\/?(?!\!)[^>]*>/gi, ''); } else { var allowed = arguments[1]; var specified = eval("["+arguments[2]+"]"); if(allowed){ var regex='</?(?!(' + specified.join('|') + '))\b[^>]*>'; html=html.replace(new RegExp(regex, 'gi'), ''); } else{ var regex='</?(' + specified.join('|') + ')\b[^>]*>'; html=html.replace(new RegExp(regex, 'gi'), ''); } } //CHANGE NAME TO CLEAN JUST BECAUSE var clean_string = html; //RETURN THE CLEAN STRING return clean_string;
If you are using this function you probably know your way around JavaScript well enough you don’t need a complete example but if you do here is a quick and dirty one:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
/***************************************************
STRIP HTML TAGS
****************************************************/
function strip_tags(html){
//PROCESS STRING
if(arguments.length < 3) {
html=html.replace(/<\/?(?!\!)[^>]*>/gi, '');
} else {
var allowed = arguments[1];
var specified = eval("["+arguments[2]+"]");
if(allowed){
var regex='</?(?!(' + specified.join('|') + '))\b[^>]*>';
html=html.replace(new RegExp(regex, 'gi'), '');
} else{
var regex='</?(' + specified.join('|') + ')\b[^>]*>';
html=html.replace(new RegExp(regex, 'gi'), '');
}
}
//CHANGE NAME TO CLEAN JUST BECAUSE
var clean_string = html;
//RETURN THE CLEAN STRING
return clean_string;
}
</script>
</head>
<script type="text/javascript">
var sample_html = '<h1><a href="http://www.gozipline.com">Zipline Interactive</a></h1>';
document.write('Without Stripping Tags : ' + sample_html);
var demo = strip_tags(sample_html);
document.write('With Stripping Tags: ' + demo);
</script>
<body>
</body>
</html>No related posts.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.


Good job. When are you adding the ‘allowable_tags’ param?