开发者

Javascript: strip tags ONLY IF tags found

开发者 https://www.devze.com 2023-01-31 09:55 出处:网络
EDIT: I have a function that wraps a string between tags. I want this function to apply only if the string does not contain any tags.

EDIT: I have a function that wraps a string between tags. I want this function to apply only if the string does not contain any tags.

if string: "text" then "<b>text</b>";  
else if string "<b>text</b>" then "text";

I ne开发者_高级运维ed a conditional statement that checks for given tags and strips the tags only if tags are found.

eg. function stripTags(string, "span")

1- search for given tags (span in this case)

2- if found, strip tags


function stripTags(string, tag) {
  var tagMatcher = new RegExp('</?' + tag + '>','g');
  return string.replace(tagMatcher, '');
}

to remove any tag from the string or

function toggleSurroundingTags(string, tag) {
  var tagMatcher = new RegExp('^<' + tag + '>(.*)</' + tag + '>$');
  var match = tagMatcher.exec(string);
  if (match) {
    return match[1];
  } else {
    return '<' + tag + '>' + string + '</' + tag + '>';
  }
}

To remove surrounding tags if they exist and add them if they don't exist:

toggleSurroundingTags('hello', 'b'); // returns '<b>hello</b>'
toggleSurroundingTags('<b>hello</b>', 'b'); // returns 'hello'


Check out:

  • PHPJS's strip_tags

Source:

function strip_tags (input, allowed) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Luke Godfrey
    // +      input by: Pul
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +      input by: Alex
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Marc Palau
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Eric Nagel
    // +      input by: Bobby Drake
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Tomasz Wesolowski
    // +      input by: Evertjan Garretsen
    // +    revised by: Rafał Kukawski (http://blog.kukawski.pl/)
    // *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
    // *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
    // *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
    // *     returns 2: '<p>Kevin van Zonneveld</p>'
    // *     example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
    // *     returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
    // *     example 4: strip_tags('1 < 5 5 > 1');
    // *     returns 4: '1 < 5 5 > 1'
    // *     example 5: strip_tags('1 <br/> 1');
    // *     returns 5: '1  1'
    // *     example 6: strip_tags('1 <br/> 1', '<br>');
    // *     returns 6: '1  1'
    // *     example 7: strip_tags('1 <br/> 1', '<br><br/>');
    // *     returns 7: '1 <br/> 1'

       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,
           commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
       return input.replace(commentsAndPhpTags, '').replace(tags, function($0, $1){
          return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
       });
    }


I assume you're looking for a javascript answer. I'm not a jQuery guy... This makes use of the dom, so the browser's implementation may end up changing the case or dropping attributes it doesn't recognize...

function stripTags(string) {
    if (!arguments.callee._tempNode) {
        arguments.callee._tempNode = document.createElement('div');
    }
    var node = arguments.callee._tempNode;
    node.innerHTML = string;

    var i=arguments.length;
    while (--i) { // decrement first so the string var won't be processed
        var toRemove = node.getElementsByTagName(arguments[i]);
        while (toRemove.length) {
            toRemove[0].parentNode.removeChild(toRemove[0]);
        }        
    }
    return node.innerHTML;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号