青春有我
2023-06-15 16:43:19
我需要替換我的字符串中的所有撇號,只有 href=" " 標簽中的那些,外面的撇號應該保留。我正在使用 node.js。這是我的字符串:This is an example text. I'd love to fix this. <a href="https://www.example.com/i'd-love-to-fly/"> I'd love to fly link </a> Inside the text there could be more urls <a href="https://www.example.com/i'd-rather/">I'd rather link</a>. 例如<a href="https://www.example.com/i'd-love-to-fly/"> I'd love to fly link </a> 應該<a href="https://www.example.com/id-love-to-fly/"> I'd love to fly link </a> 我正在嘗試使用正則表達式/https[^"]++/它選擇了整個 URL,但我不知道如何只選擇和替換撇號
2 回答

12345678_0001
TA貢獻1802條經驗 獲得超5個贊

慕婉清6462132
TA貢獻1804條經驗 獲得超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);
添加回答
舉報
0/150
提交
取消