I am making a chrome extension that is given a list of domains that needs to be compared against the active URL of a tab. For example if the list of domains has "google" then the extension should detect "docs.google.com" as part of the domain list. I have gotten this part to work. The issue is when the domain list contains a subdomain. For example: if "docs.google" is on the list then if the user is on "google.com" the extension should not recognize this as a URL on the domain list.
I am attempting this by constructing a regular expression. for each domain and subdomain. As I said, when you are given a domain (as opposed to a subdomain) it works properly although I have tested this with subdomains and it does not seem to work. I assume the issue is with how I constructed the RegEx. Anything that stands out? thank you in advance!
let onDomainList = false;
for(let i = 0; i < domainListLength-1; i++){
if(!domainList[i].includes(".")){ //if this domain is not a subdomain
let strPattern = "^https://www\\." + list.domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "|https://[a-z_]+\\." + list.domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let domainRegEx = new RegExp(strPattern,'i');
if(domainRegEx.test(activeTab.url)){
onDomainList = true;
execute_script(activeTab);
}
} else{ //if this domain is a subdomain
let strPattern = "^https://www\\." + list.domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let domainRegEx = new RegExp(strPattern,'i');
if(domainRegEx.test(activeTab.url)){
onDomainList = true;
execute_script(activeTab);
}
}
}
EDIT: Changed RegEx to what Wiktor Stribizew suggested, although still the issue of not detecting subdomains.