【问题标题】:Regex remove hyperlinks for a specific domain正则表达式删除特定域的超链接
【发布时间】:2018-11-30 00:01:13
【问题描述】:

如果 URL 与特定域匹配,我如何从链接 HTML 元素中提取文本?

例如从以下位置提取hello

<a href="https://example.com/2018/11/22/ff/">hello</a>

如果 URL 不是 example.com,那么它应该忽略它。

我正在使用正则表达式 &lt;/?a(|\s+[^&gt;]+)&gt;,但它适用于所有域,而它应该只适用于 example.com

【问题讨论】:

标签: javascript php html regex


【解决方案1】:

我是一个菜鸟开发者!但我认为这对你有用!

//The website you want to block
var regex = /example.com/g
// Select all anchor text from the window
var textLink = document.querySelectorAll("a");

//Check all anchor from the windows if they contain the "example.com" if they do, the href will be replace with "#"
textLink.forEach(  e => {
    if(regex.test(e.href)){
    e.href = "#";
}});

【讨论】:

    猜你喜欢
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2010-10-23
    • 2010-10-31
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多