Strip Specific HTML Tags From String

In fiddling around with some code highlighters to use for this blog, I found myself needing a way to strip tags from a JavaScript string of HTML, while leaving others. I needed it so I could convert highlighted code to plain text so user’s could copy and paste code snippets without all the HTML garbage. This is what I came up with:

var saveTags = new RegExp('<(?!((\/)?(li|span)))(.)?([a-zA-Z?])[^><]*>', 'gi');

var html = '<div><ol><li><span>Some Text</span></li></ol></div>';
var strippedHtml = html.replace(saveTags, '');

alert(html);          // <div><ol><li><span>Some Text</span></li></ol></div>
alert(strippedHtml);  // <li><span>Some Text</span></li>

The regex matches all html tags except li and span. You then simply use the String method ‘replace‘ to replace the matches with an empty string. To edit the tags for the regex, simply change the tags in the expression, making sure to separate them with the bar ‘|’ character.

After figuring out how to strip the specific tags , I didn’t even end up using it because I moved from jQuery Chili 2.2 to SyntaxHighlighter which has a built in source code viewer, but I thought the information was valuable nonetheless.

This entry was posted in JavaScript and tagged , , , , , . Bookmark the permalink.

2 Responses to Strip Specific HTML Tags From String

  1. Josh Dean says:

    Perfect, this’ll come in handy! I needed a way to strip ALL HTML tags in JavaScript.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>