为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信