2 回答

TA貢獻1802條經(jīng)驗 獲得超5個贊
假設您在 URL 中只有一個單引號:
(href="[^"]+)'([^"]+")
https://regex101.com/r/6zRLSC/1
如果您有多個單引號,則 while 循環(huán)可以解決該問題。

TA貢獻1804條經(jīng)驗 獲得超2個贊
var string = `This is an example text. I'd love to fix this.
<a href="https://www.example.com/i'd-love-to-fly-but-don't-like-heights/"> I'd love to fly but don't like heights link </a>
Inside the text there could be more urls
<a href="https://www.example.com/i'd-rather/">I'd rather link</a>.`;
// While we match the pattern that we're targeting, keep on replacing
while(string.match(/(href="[^"]+)'([^"]+")/))
{
string = string.replace(/(href="[^"]+)'([^"]+")/g, '$1$2')
}
console.log(string);
添加回答
舉報