I'd like to invoke some Javascript only if I'm on a specific URL with a random subdomain: https://*.testsite123.io/testsite123/portal/#/login
. So far I came up with
function escapeRegExp(str) {
var escaped = str.replace(/([.+?^=!:${}()|\[\]\/\\])/g, "\\$1");
console.log("escaped " + escaped);
escaped = new RegExp(escaped);
return escaped;
}
function matchUrl() {
var currentUrl = window.location.toString();
var loginUrlRegex = escapeRegExp("https://*.testsite123.io/testsite123/portal/#/login");
return loginUrlRegex.test(currentUrl);
}
console.log("window.location " + window.location.toString());
console.log("match " + matchUrl());
Executed in the chrome console while on https://prod.testsite123.io/testsite123/portal/#/login
this yields:
window.location https://prod.testsite123.io/testsite123/portal/#/login
escaped https\:\/\/*\.testsite123\.io\/testsite123\/portal\/#\/login
match false
I don't know why they don't match? I've escaped everything except the *
?