我正在使用以下正则表达式来匹配文本中的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)
尝试使用:
RegExp("(^|[\s.:;?\-\]<\(])(https?://[-\w;/?:@&=+$\|\_.!~*\|'()\[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',\|\(\).:;?\-\[\]>\)])","i")
据我所知,括号不是出现在URL中的有效字符(至少不是未编码)
此问题中有很多针对不同用例的RegEx模式:
What is the best regular expression to check if a string is a valid URL?