正则表达式以匹配URL,当URL在括号之间时,最终匹配URL加上右括号

问题描述 投票:-1回答:1

我正在使用以下正则表达式来匹配文本中的URL

new RegExp(`(https?://\\S+\\.\\S+)\\s`, 'ig');

问题是URL出现在这样的括号中

(e.g. https://hello.com/fridaysforfuture-eu-socialmedia)

它在URL中摘录了右括号

我的预期行为是,如果网址类似,则在比赛中加入右括号

https://hello.com/name-(1)

但是如果URL在这样的括号内,则不包括右括号

(e.g. https://hello.com/fridaysforfuture-eu-socialmedia)

javascript regex pattern-matching string-matching regex-group
1个回答
0
投票

尝试使用:

RegExp("(^|[\s.:;?\-\]<\(])(https?://[-\w;/?:@&=+$\|\_.!~*\|'()\[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',\|\(\).:;?\-\[\]>\)])","i")

参考:https://mathiasbynens.be/demo/url-regex


0
投票

据我所知,括号不是出现在URL中的有效字符(至少不是未编码)

此问题中有很多针对不同用例的RegEx模式:

What is the best regular expression to check if a string is a valid URL?

© www.soinside.com 2019 - 2024. All rights reserved.