[英]Find word in link regex
是否可以使用正則表達式匹配包含特定文本的鏈接? 我需要在href值后添加一些文本。 我嘗試在以下鏈接中匹配“單詞”:
<a href="http://word/sth">some link</a>
我嘗試過,但是沒有成功:
(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.