簡體   English   中英

在鏈接正則表達式中查找單詞

[英]Find word in link regex

是否可以使用正則表達式匹配包含特定文本的鏈接? 我需要在href值后添加一些文本。 我嘗試在以下鏈接中匹配“單詞”:

<a href="http://word/sth">some link</a>

我嘗試過,但是沒有成功:

(https?:\/\/[^word"]+)

實際上,您實際上不需要^和$,方括號也要匹配給定集合中的單個字符。

嘗試一下:

https?:\/\/(word)

有用的資源:您可以在此處測試您的正則表達式

如果這是一個選項,則可以使用DOM而不是正則表達式:

 var div = document.createElement('div'); div.innerHTML = '<a href="http://word/sth">some link</a>'; if (div.firstChild.innerHTML.indexOf("some") !== -1) { console.log("contains some!"); div.firstChild.href += " added text"; } console.log(div.firstChild); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM