Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added string.stripTags()

  • Loading branch information...
commit bed76b7803cb419c0919165300582288b697ab8b 1 parent edae82a
@der-On der-On authored
Showing with 19 additions and 0 deletions.
  1. +19 −0 lib/string.js
View
19 lib/string.js
@@ -784,6 +784,25 @@ string = new (function () {
return uuid.join('');
};
+
+ /**
+ @name string#stripTags
+ @public
+ @function
+ @return {String} A String with HTML tags removed.
+ @description Strips HTML tags from a string.
+ @param {String} string The string to strip HTML tags from
+ @param {String|Array} A String or Array containing allowed tags. e.g. "<br><p>"
+ */
+ this.stripTags = function(string, allowed) {
+ // taken from http://phpjs.org/functions/strip_tags/
+ var allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
+ var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
+ comments = /<!--[\s\S]*?-->/gi;
+ return input.replace(comments, '').replace(tags, function ($0, $1) {
+ return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
+ });
+ }
})();
Please sign in to comment.
Something went wrong with that request. Please try again.