2014-10-07 229 views
0

我想和JavaScript更換.html擴展一個網頁,不包含像http://https://協議,以確保它們是本地鏈接中的鏈接。HREF鏈接擴展

我已經試過這樣的事情,但不能正常工作:

字符串

<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 

目前尚不清楚,我裏面怎麼否定協議正則表達式,如果我把它們沒有任何匹配。

回答

1

幾乎沒有,你只是錯過了負前瞻:

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

演示在這裏:http://regex101.com/r/hA4fP1/1

+0

謝謝,它完美的作品 – vitto 2014-10-07 13:52:52

1

下面的正則表達式將捕獲本地url路徑。

href="((?:(?!https?:\/\/).)+?)\.html" 

DEMO