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.
Test Comment
Perfect, this’ll come in handy! I needed a way to strip ALL HTML tags in JavaScript.