对于不包含http://
和https://
等协议的网页内的链接,我想用JavaScript
替换.html
扩展,以确保它们是本地链接。
我试过这样的东西,但无法正常工作:
字符串
<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
我不清楚如何在regex中否定协议,如果我放置它们,则没有匹配的协议。
快到了,你只是错过了负面展望:
href="(?!(?:http:|https:))(.*?).html"
此处演示:http://regex101.com/r/hA4fP1/1
下面的正则表达式将捕获本地url路径。
href="((?:(?!https?://).)+?).html"
演示
(href="(?!http)[^>]*/.*?).html
试试这个。用$1Javascript
替换。请参阅演示。
http://regex101.com/r/hQ1rP0/39