jQuery: stripping HTML tags

In this post I'm going to show you how to implement the PHP function strip_tags() in jQuery. As you will see, all the work is done by two regular expressions, one for matching an opening tag and the other for matching the corresponding closing tag. We define our method as global directly under the $ wrapper, like so:

(function($) {

  $.stripTags = function(value) {

     var startTag = /<([a-z][a-z0-9]*)\b[^>]*>/gmi;
     var endTag = /<\/([a-z][a-z0-9]*)\b[^>]*>/gmi;

     var stripped = value.replace(startTag, '').replace(endTag, '');

     return stripped;



As you can see, the two regular expressions must be set with the flags g and i to match tags at a global level and with an arbitrary case, respectively. Let's see the stripTags() function in action:

$(document).ready(function() {

   $('#test').submit(function(e) {

       var value = $(this).find('#markup').val();
       var newVal = $.stripTags(value);





You can test this code below.


Live demo

This entry was posted in by Gabriele Romanato. Bookmark the permalink.

Comments are closed.