我有下面的正则表达式
"^http\\\\://[a-zA-Z0-9\\\\-\\\\.]+\\\\.[a-zA-Z]{2,3}(/\\\\S*)?$";
但是我不确定它是否正确地验证了URL。有人能帮助我或者看看这有什么问题吗?谢谢
hgb9j2n61#
如果你想要一个实心图案read here。看起来像Rakesh一些很好的MOD到您现有的模式;但是,如果我是您,我会考虑上述模式,因为根据您的场景,它们更健壮一些。
nlejzf6q2#
试试这个,在你的版本中有很多转义符“/”
var subUrlSTR = "http://subdomain.stackoverflow.com"; var urlSTR = "http://stackoverflow.com"; var result = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/; console.log(subUrlSTR.match(result)); console.log(urlSTR.match(result));
看到它working here
7hiiyaii3#
if (Uri.TryCreate(stringUrl, UriKind.Absolute, out uri)) { ... }
yk9xbfzb4#
覆盖99%以上的敕勒浏览器支持的URL,包括:
https?:\/\/(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}\b|([-a-zA-Z0-9\u1F60-\uFFFF\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F@:%._\+~#=]{1,256})\.([a-zA-Z][a-zA-Z0-9\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F\u0370-\u03ff\u1f00-\u1fff\u0400-\u04ff()-]{1,62}))\b([\/#][-a-zA-Z0-9\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F\u0370-\u03ff\u1f00-\u1fff\u0400-\u04ff\u0900-\u097F\u0600-\u06FF\u0985-\u0994\u0995-\u09a7\u09a8-\u09ce\u0981\u0982\u0983\u09e6-\u09ef\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF\u4E00-\u9FFFẸɓɗẹỊỌịọṢỤṣụ()@:%_\+.~#?&//=\[\]!\$'*+,;]*)?
https?:\/\/([-a-zA-Z0-9\u1F60-\uFFFF\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F@:%._\+~#=]{1,256})\.([a-zA-Z][a-zA-Z0-9\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F\u0370-\u03ff\u1f00-\u1fff\u0400-\u04ff()-]{1,62})\b([\/#][-a-zA-Z0-9\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F\u0370-\u03ff\u1f00-\u1fff\u0400-\u04ff\u0900-\u097F\u0600-\u06FF\u0985-\u0994\u0995-\u09a7\u09a8-\u09ce\u0981\u0982\u0983\u09e6-\u09ef\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF\u4E00-\u9FFFẸɓɗẹỊỌịọṢỤṣụ()@:%_\+.~#?&//=\[\]!\$'*+,;]*)?
([-a-zA-Z0-9\u1F60-\uFFFF\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F@:%._\+~#=]{1,256})\.([a-zA-Z][a-zA-Z0-9\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F\u0370-\u03ff\u1f00-\u1fff\u0400-\u04ff()-]{1,62})\b([\/#][-a-zA-Z0-9\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F\u0370-\u03ff\u1f00-\u1fff\u0400-\u04ff\u0900-\u097F\u0600-\u06FF\u0985-\u0994\u0995-\u09a7\u09a8-\u09ce\u0981\u0982\u0983\u09e6-\u09ef\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF\u4E00-\u9FFFẸɓɗẹỊỌịọṢỤṣụ()@:%_\+.~#?&//=\[\]!\$'*+,;]*)?
// Check with a simple copy/paste in console! const regexURLsAndIPs = /^https?:\/\/(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}\b|([-a-zA-Z0-9\u1F60-\uFFFF\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F@:%._\+~#=]{1,256})\.([a-zA-Z][a-zA-Z0-9\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F\u0370-\u03ff\u1f00-\u1fff\u0400-\u04ff()-]{1,62}))\b([\/#][-a-zA-Z0-9\u1F60-\uFFFF\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u024F\u0370-\u03ff\u1f00-\u1fff\u0400-\u04ff\u0900-\u097F\u0600-\u06FF\u0985-\u0994\u0995-\u09a7\u09a8-\u09ce\u0981\u0982\u0983\u09e6-\u09ef\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF\u4E00-\u9FFFẸɓɗẹỊỌịọṢỤṣụ()@:%_\+.~#?&//=\[\]!\$'*+,;]*)?$/ const shouldMatch = [ 'https://base.com/', 'http://t.co', 'https://www.google.com.ua/', 'https://subdomains.as.deep.as.you.want.example.com', 'https://sub.second_leveldomain_underscore.verylongtoplevedomain/nice', 'https://domain-name.com/path-common-characters/ABCxyz01789', 'http://domaîn-with-àccents.ca', 'http://path-with-accents.com/àèìòùçÇßØøÅåÆæœ', 'https://en.wikipedia.org/wiki/Möbius_strip', 'http://www.😉.tld/emojis-🤖-in-👏-domain/-and-path-🚀/', 'https://y.at/🚀🚀🚀', 'https://hashtag.forpath#lets-go', "http://special.com/all-special-characters-._~:/?#[]@!$&'()*+,;=", 'https://greek_with_diacritics.co/ΑαΒβΣσ/ςΤτϋΰήώΊΪΌΆΈΎΫΉΏᾶἀ', 'https://el.wikipedia.org/wiki/Ποσειδώνας_(πλανήτης)', 'http://cyrillic-and-extras.ru/АаБбВвЪъыӸӹЫЯЯяѶѷ', 'https://ru.wikipedia.org/wiki/Заглавная_страница', 'https://most-arabic.co/گچپژیلفقهموء-يجريبتج/', 'https://urdu.co/حروفِ/', 'https://nigerian.ni/ƁƊƎẸɓɗǝẹỊƘỌịƙọṢỤṣụ', 'https://bengali.sports.co/স্পর্শঅনুনাসিকলসওষ্ঠ্যপফবভম/', 'https://devenagri.cc/कखगघङचछजझञटठडढणतथदधनपफबभमयरलवशषस', 'https://h.org/wiki/Wikipedia:关于中文维基百科/en', 'https://zh.wikipedia.org/wiki/Wikipedia:关于中文维基百科/en', 'http://others.kr/korean-안녕ㆅㅇㄹㅿㆍㅡㅣㅗㅑㅠㅕ/japanese-一龠ぁゔazAZ09々〆〤ヶ', 'https://龠.subdomain.com', 'http://127.0.0.1:22/valid-ip', 'http://127.00.00.01:22/ugly-but-still-works-with-modern-browsers', 'http://0.0.0.0:0/is-min', 'https://255.255.255.255:0/is-max', 'https://this.tld-is-63-characters-wich-is-the-theoretical-limit-000000000000', ] const shouldNotMatch = [ 'noprotocol.com', ' https://space-in-front.com', 'https://invalid.0om', 'https://invalid.-om', 'https://invalid-single-letter-tld.c', 'https://invalid-domain&char.com', 'https://invalid:com', 'https://not valid.com', 'https://not,valid.com', 'https://龠.c龠', 'https://invalidαΒβΣσ.com', 'notvalid://www.google.com', 'http://missing-tld', 'https://0.0.0.0missing-port', '0.0.0.0:0/missing-protocol', 'https://256.255.255.255:0/is-above-max', 'https://this.tld-is-64-characters-which-is-too-looooooooooooooooooooooooooong', ] function checkStringsMatchRegex(regex, array, shouldMatch = true) { for (let i = 0; i < array.length; i++) { if (regex.test(array[i]) !== shouldMatch) { const matchStr = shouldMatch ? 'match' : 'not match' console.error('regex.test(array[i])', regex.test(array[i])) throw new Error(`String "${array[i]}" should ${matchStr} regex "${regex}"`) } } const successMatchingStr = shouldMatch ? 'matching all strings' : 'not matching a single string' console.log(`Success with ${successMatchingStr} in the test array.`) } checkStringsMatchRegex(regexURLsAndIPs, shouldMatch, true) checkStringsMatchRegex(regexURLsAndIPs, shouldNotMatch, false)
4条答案
按热度按时间hgb9j2n61#
如果你想要一个实心图案read here。
看起来像Rakesh一些很好的MOD到您现有的模式;但是,如果我是您,我会考虑上述模式,因为根据您的场景,它们更健壮一些。
nlejzf6q2#
试试这个,在你的版本中有很多转义符“/”
看到它working here
7hiiyaii3#
yk9xbfzb4#
2023年及以后的国际化、现代化答案
覆盖99%以上的敕勒浏览器支持的URL,包括:
使用此选项包含协议(“http/https”)和IPv4 URL:
使用此选项可包含协议地址,但不包含IPv4地址:
匹配域名和路径(无IPv4):
经过所有这些现实世界和理论边缘案例的实战测试: