forked from freeCodeCamp/boilerplate-project-urlshortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlvalidator.js
25 lines (24 loc) · 1008 Bytes
/
urlvalidator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module.exports = class UrlValidator {
constructor() {
this.protocol = "https?://";
const subdomain = "[a-zA-Z0-9-]+";
const domain = "[a-zA-Z0-9-]+";
const topDomain = "[a-zA-Z0-9-]+";
const query = "[a-zA-Z0-9-]+=[a-zA-Z0-9-]+";
this.queryList = `\\?${query}(&${query})*`;
const subdir = "/[a-zA-Z0-9-]+"
this.subdirectories = `${subdir}(${subdir})*`;
this.regex =
new RegExp(`^${this.protocol}(${subdomain}\.)?${domain}\.${topDomain}(${this.subdirectories})?/?(${this.queryList})?$`);
}
test(url) {
return this.regex.test(url);
}
getDomain(url){
return url.replace(new RegExp(this.protocol),"").replace(new RegExp("^(.*?)[/\?].*?$"),"$1");
}
}
//export {UrlValidator};
//console.log(new RegExp(`\\?asdf`).test("?asdf"))
//console.log(new UrlValidator().test("https://www.google.com/asdf/asdf/?asdf=1&asdf=2"));
//console.log(new UrlValidator().getDomain("https://www.google.com"));