I have the following Regex
"^http\\\\://[a-zA-Z0-9\\\\-\\\\.]+\\\\.[a-zA-Z]{2,3}(/\\\\S*)?$";
But I'm not sure that it's validating URLs correctly. Is anyone able to assist me or see what's wrong with this?
Thanks
I have the following Regex
"^http\\\\://[a-zA-Z0-9\\\\-\\\\.]+\\\\.[a-zA-Z]{2,3}(/\\\\S*)?$";
But I'm not sure that it's validating URLs correctly. Is anyone able to assist me or see what's wrong with this?
Thanks
If you want a solid pattern read here.
Looks like Rakesh some good mods to your existing pattern; however, if I were you I would consider the aforementioned patterns because they are a bit more robust depending on your scenario.
Try this, there a quite a bit of escapes "/" in your version
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));
See it working here
if (Uri.TryCreate(stringUrl, UriKind.Absolute, out uri))
{
...
}
Covering 99%+ URLs supported by modern browsers, that includes:
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)