Javascript Function: strip_tags()

A useful function to mimic to PHP function strip_tags() to strip all HTML tags and markup from a string.

Useful when parsing client input on the fly. Extra checks should be added though.


The code:

function strip_tags(str) {
   return str.replace(/<\/?[^>]+>/gi, "");
}

An example:

alert(strip_tags('<strong><em>See the HTML around this message?</em></strong>'));
// The html tags won't show in this message.

Hope you find this useful in your projects.

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.

Comments

No comments yet.

Leave a comment

(required)

(required)