简体   繁体   English

如何从 javascript 中的正则表达式组中省略一些文本?

[英]How to omit some text from regex group in javascript?

let regexp = /(\b(."https?|ftp|file):\/\/[-"A-Z0-9+&@#\/%?=~_|:,.;;]*[-A-Z0-9+&@#\/%=~_|])/i

let stringData = `<p><a href="https://domainName.com/chat/conversationId">https://dev.uniteliving.com/chat/bk7RELvbh7KrvJ6ii#</a>`;

Here matching ="https this string and group ="https://domainName.com/chat/conversationId .这里匹配="https这个字符串和组="https://domainName.com/chat/conversationId

I want to omit =" from the group. Actually need to https://domainName.com/chat/conversationId .我想从组中省略=" 。实际上需要https://domainName.com/chat/conversationId

Please any body suggest me.请任何机构建议我。 How to solved it?如何解决?

Demo演示

Use this regex:使用这个正则表达式:

/(\b((?<=")https?|ftp|file):\/\/[-"A-Z0-9+&@#\/%?=~_|:,.;;]*[-A-Z0-9+&@#\/%=~_|])/i

Test it on regex101在 regex101 上测试它

What I changed is use a lookbehind (?<=") instead of a simple match for this character " you don't want.我改变的是使用后视(?<=")而不是简单匹配这个字符"你不想要。

The original regex was also trying to match "any" character before the " , I removed this as I don't think it is useful (at least it's not necessayr with your example).原始的正则表达式还试图匹配 " 之前的"任何”字符,我删除了它,因为我认为它没有用(至少它不是你的例子所必需的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM