亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Node.js 正則表達式刪除 url 中的撇號

Node.js 正則表達式刪除 url 中的撇號

青春有我 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個贊

假設您在 URL 中只有一個單引號:

(href="[^"]+)'([^"]+")

https://regex101.com/r/6zRLSC/1

如果您有多個單引號,則 while 循環可以解決該問題。


查看完整回答
反對 回復 2023-06-15
?
慕婉清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);


查看完整回答
反對 回復 2023-06-15
  • 2 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號