Ralph Holzmann

Web Development – jQuery, PHP, MySQL. In that order.

Strip Specific HTML Tags From String December 10th, 2009

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.

2 Comments Time: Relative | Actual

  1. Ralph said:

    Test Comment

  2. Josh Dean said:

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

Other stuff …

Tweets I write

Posts I write

Companies I currently develop for

Past development gigs

Things I use everyday

Ways to contact me