我需要使用JavaScript验证已知域名,例如 domain.com ,但我需要使用任何子域或子子域验证它,以及任何协议和任何文件夹,子文件夹,所以,我有这个正则表达式:
RegEx: /http:\/\/([\.|a-z]+).domain([\.|a-z]+)\//
问题是它没有获得https / ftp / ftps ...而且它获得域 .com / 域 。有机 ... 我需要验证,如下所示:
To validate: *://*.domain.com/*
Example: http://example.domain.com/subfolder2/folder24/
Example: https://subdomain.domain.com/folder/
Example: ftp://www.example.domain.com/folder2/folder/
Example: http://www.domain.com/folder/sub-folder/
我的意思是,任何协议,任何子域和任何文件夹,当然,所有这些都在一起:
Example: https://example.domain.com/folder/folder/
有什么想法吗?
答案 0 :(得分:3)
试试这个正则表达式:
/^(https?|ftp):\/\/[.a-z]+\.domain\.com[.a-z0-9/-]+/
这将与您所感兴趣的域名相匹配:
http://example.domain.com/subfolder2/folder24/
https://subdomain.domain.com/folder/
ftp://www.example.domain.com/folder2/folder/
http://www.domain.com/folder/sub-folder/
工作原理:
# / - Regex start,
# ^ - Match the start of the string,
# (https?|ftp) - followed by `https`, `http` or `ftp`,
# :\/\/ - followed by `://`,
# [.a-z]+ - followed by any amount of letters or periods,
# \. - followed by a period,
# domain\.com - followed by the domain name,
# [.a-z0-9/-]+ - followed by the rest of a possible url.
# / - regex end.