1

我需要使用 JavaScript 验证已知域,例如domain.com,但我需要使用任何子域或子子域等验证它,以及使用任何协议以及任何文件夹、子文件夹等等,我有这个正则表达式:

RegEx: /http:\/\/([\.|a-z]+).domain([\.|a-z]+)\//

问题是它没有得到 https/ftp/ftps ......它得到了domain .com / domain .org ......我需要验证,像这样:

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/

任何的想法?

4

1 回答 1

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.
于 2013-02-11T11:03:27.890 回答