What's the best way to strip out only the anch

2019-08-27 07:59发布

Let's say there's a string of HTML, with script tags, plain text, whatever.

What's the best way to strip out only the <a> tags?

I've been using some methods here, but these are for all tags. Strip HTML from Text JavaScript

4条回答
戒情不戒烟
2楼-- · 2019-08-27 08:01

A <a> tag is not supposed to hold any other <a> tag, so a simple ungreedy regexp would do the trick (i.e. string.match(/<a>(.*?)<\/a>/), but this example suppose the tags have no attribute).

查看更多
倾城 Initia
3楼-- · 2019-08-27 08:04

Using jQuery:

var content = $('<div>' + htmlString + '</div>');
content.find('a').replaceWith(function() { return this.childNodes; });
var newHtml = content.html();

Adding a wrapping <div> tag allows us to get the desired HTML back.

I wrote a more detailed explanation on my blog.

查看更多
时光不老,我们不散
4楼-- · 2019-08-27 08:12

Here's a native (non-library) solution if performance is a concern.

function stripTag(str, tag) {
    var a, parent, div = document.createElement('div');
    div.innerHTML = str;
    a = div.getElementsByTagName( tag );
    while( a[0] ) {
        parent = a[0].parentNode;
        while (a[0].firstChild) {
            parent.insertBefore(a[0].firstChild, a[0]);
        }
        parent.removeChild(a[0]);
    }
    return div.innerHTML;
}

Use it like this:

alert( stripTag( my_string, 'a' ) );
查看更多
Emotional °昔
5楼-- · 2019-08-27 08:22

This approach will preserve existing DOM nodes, minimizing side-effects if you have elements within the anchors that have events attached to them.

function unwrapAnchors() {
    if(!('tagName' in this) || this.tagName.toLowerCase() != 'a' || !('parentNode' in this)) {
        return;
    }
    var childNodes = this.childNodes || [], children = [], child;
    // Convert childNodes collection to array
    for(var i = 0, childNodes = this.childNodes || []; i < childNodes.length; i++) {
        children[i] = childNodes[i];
    }
    // Move children outside element
    for(i = 0; i < children.length; i++) {
        child = children[i];
        if(('tagName' in child) && child.tagName.toLowerCase() == 'a') {
            child.parentNode.removeChild(child);
        } else {
            this.parentNode.insertBefore(child, this);
        }
    }
    // Remove now-empty anchor
    this.parentNode.removeChild(this);
}

To use (with jQuery):

$('a').each(unwrapAnchors);

To use (without jQuery):

var a = document.getElementsByTagName('a');
while(a.length) {
    unwrapAnchors.call(a[a.length - 1]);
}
查看更多
登录 后发表回答