剥离所有<a> tags except those that start with http

时间:2018-09-10 21:58:34

标签: html regex

I would like to remove all tags that start with href="http: and leave the text being linked in place for each of them.

There are other tags in my html file that I don't want changed at all. Again, I only want the tags that start with http stripped with the text being linked staying in place.

Essentially I want all external links stripped from the html document and the internal links to remain intact.

Any help with the right Find What: and Replace with: information would be appreciated!

3 个答案:

答案 0 :(得分:2)

您可以删除所有不包含当前主机的标签:

const links = Array.from(document.getElementsByTagName('a'))
links.forEach(elm => {
  !elm.href.includes(window.location.host) && elm.parentNode.removeChild(elm);
})
<a href="https://google.com">google</a>
<a href="/about">about</a>

不需要JQuery

答案 1 :(得分:0)

解决方案可能类似于

jQuery( document ).ready( function($) {

//stores all a tags in an array.
var aTags = document.getElementsByTagName("a");
var re = new RegExp("^http://");
var length = aTags.length;

for(var i = 0; i < length; i++) {
    if( aTags[i].href.test(re) ) {
         aTags[i].href = "https://" //reset to something else if true?

         //OR

         aTags[i].href.replace("http://", "https://"); //replace with https
    }
}

});

答案 2 :(得分:0)

以下JavaScript应将所有链接替换为包含以下内容的文本:

var links = document.getElementsByTagName("a");
var regEx = /^https?:\/\//;

for (var i = 0; i < links.length; i++)
{
    var elem = links[i];
    if (elem.href.test(regEx))
    {
        var node = document.createTextNode(elem.textContent);
        elem.parentElement.replaceChild(node, elem);
    }
}

它循环遍历所有链接,并且如果它们以'http://'或'https://'开头,则会使用文本创建一个textnode,然后该textnode替换链接。