Javascript 正则表达式仅匹配具有tld的URL
我知道有无数的正则表达式用于验证URL,但我发现了一个问题。我需要一个正则表达式,它将匹配所有Http或Https URL,但如果链接没有tld则不验证,如果缺少子域则不验证。如果可能,它还需要匹配完整的url段塞 以下是不应验证的URL示例:Javascript 正则表达式仅匹配具有tld的URL,javascript,regex,Javascript,Regex,我知道有无数的正则表达式用于验证URL,但我发现了一个问题。我需要一个正则表达式,它将匹配所有Http或Https URL,但如果链接没有tld则不验证,如果缺少子域则不验证。如果可能,它还需要匹配完整的url段塞 以下是不应验证的URL示例: https://www https://www. https://www.exampledomain https://www.axiamed.com https://www.axiamed.com/independent-software-vendor
https://www
https://www.
https://www.exampledomain
https://www.axiamed.com
https://www.axiamed.com/independent-software-vendors/
下面是几个应该验证的URL示例:
https://www
https://www.
https://www.exampledomain
https://www.axiamed.com
https://www.axiamed.com/independent-software-vendors/
我一直在使用下面的正则表达式
((ftp|https?)://[-\w]+(\.\w[-\w]*)+|(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?: com\b|edu\b|biz\b|gov\b|in(?:t|fo)\b|mil\b|net\b|org\b|[a-z][a-z]\b))(\:\d+)?(/[^.!,?;"'<>()\[\]{}\s\x7F-\xFF]*(?:[.!,?]+[^.!,?;"'<>()\[\]{}\s\x7F-\xFF]+)*)?
…但它得到的是上面列出的3个URL中最后一个的匹配,我不希望匹配。不知道从这里到哪里去。谁能给我指一下正确的方向吗
感谢所有愿意帮忙的人 这是解决您的问题的javascript代码示例 var result=/^http:\/\/\/www.\124https:\/\/\/www.\124http:\/\/\/\/\\/\\/\\/\\/\/\/?[a-z0-9]+[\-\.]{1}[a-z0-9]+*\.[a-z]{2,5}:[0-9]{1,5}/.*.$/。testhttps://www.axiamed.com/independent-software-vendors/; ifresult URL匹配; 其他的 alertUrl不匹配请尝试使用此选项 ^http://www.|https://www.?[a-z0-9]+[-.]{1}[a-z0-9]+.[a-z]{2,5}:[0-9]{1,5}?/$ 变量a=[ https://www.example.com, http://www.example.com, www.example.com, example.com, http://blog.example.com, http://www.example.com/product, http://www.example.com/products?id=1&page=2, http://www.example.comup, https://www, https://www., https://www.exampledomain, https://www.axiamed.com, https://www.axiamed.com/independent-software-vendors/, ]; a、 forEachfunctionurl{ var result=/^http:\/\/www.| https:\/\/www.[a-z0-9]+[\-\.]{1}[a-z0-9]+*\.[a-z]{2,5}:[0-9]{1,5}/.$/.testurl; ifresult{ console.logurl+TRUE; } 否则{ console.logurl+FALSE } }; 怎么样? /^ftp | https?\:\/{2}w{3}\.[a-z_a-z]{1,}\.[a-z]{3}\/.{1,}?/g 变量URL=[ https://www, https://www., https://www.exampledomain, https://www.axiamed.com, https://www.axiamed.com/independent-software-vendors/ ] var valids=url.mapstr=>{ 返回str.match/^ftp | https?\:\/{2}w{3}.[a-zúa-z]{1,}.[a-z]{3}/.{1,}?/g }
console.logvalids您的正则表达式完全不完整。它显然早于TLD爆炸的日期。您可以使用一个伪标记,并从中获取协议和路径属性。你愿意接受这样的解决方案吗?