javascript - 如果是本地的,则替换 href 链接扩展

标签 javascript regex replace

我想将网页内不包含 http:// 等协议(protocol)的链接的 .html 扩展名替换为 JavaScripthttps:// 以确保它们是本地链接。

我尝试过类似的方法,但无法正常工作:

字符串

<a href="local/path/to/url.html"> Replace me </a>
<a href="http://external.com/to/url.html"> DON'T replace me </a>
<a href="https://external.com/to/url.html"> DON'T replace me </a>

正则表达式

/href="^(http:|https:)(.*?).html"/gm

我不清楚如何在正则表达式中否定协议(protocol),如果我放置它们没有任何匹配。

最佳答案

快到了,你刚刚错过了否定的前瞻:

href="(?!(?:http:|https:))(.*?).html"

此处演示:http://regex101.com/r/hA4fP1/1

关于javascript - 如果是本地的,则替换 href 链接扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26234481/

相关文章:

vim -- 如何在可视 block 上执行多个搜索和替换操作?

javascript - 如何在 JavaScript 中按 block 读取本地文件?

javascript - 正则表达式尝试将除特定模式之外的任何内容与允许的模式中的字符进行匹配

regex - 为什么这个 perl prog 不打印翻译后的文本?

regex - Visual Studio - 用正则表达式替换文本

c# - 多个字符的字符串替换

javascript - (JS) 显示特定字符​​串中所有可能的字符组合,每个字符组合的输出都有一个预定的最大长度

javascript - 超出范围时如何返回 promise ?

javascript - 为什么不传递 `' '.trim( )` straight to ` [].map( )`' 的回调工作?

r - R : remove specific pattern in multiple places without removing text in between instances of the pattern 中的字符串操作