0

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 *?

mles
  • 4,534
  • 10
  • 54
  • 94
  • 1
    use RegExp literals to avoid all the confusing escaping problems – dandavis Mar 25 '17 at 23:43
  • When you say *"specific url"* at what point did `*` become anything close to specific? `https://*.`??? – zer00ne Mar 25 '17 at 23:52
  • @dandavis My URL has `/` and `#` so I don't know with any other literals I should escape it. – mles Mar 26 '17 at 00:01
  • @zer00ne sorry I meant specific url with random subdomain. the subdomain is the only thing that changes. I've updated the question. – mles Mar 26 '17 at 00:03
  • You should try adding a dot+question mark to the asterisk like so: `*.?` this will make it "lazy" meaning it'll accept whatever is `*` up until it sees the `.` – zer00ne Mar 26 '17 at 00:14

2 Answers2

1

You can use the following function to escape a regular expression in JavaScript (source):

function escapeRegExp(str) {
   return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

In your regex string, you included a *, probably as a placeholder for a substring. You need to use a correct regex variant, e.g. [^\.]+.

Furthermore, you are escaping the (incorrect) * also in the escape() function. You need to escape everything except the actual regular expression pattern. So an example for this would be:

var regexString = escapeRegExp("https://") + '[^\\.]+' + escapeRegExp(".testsite123.io/testsite123/portal/#/login");
var regex = new RegExp(regexString);
var result = regex.test(window.location.toString());

console.log(result);

In the example above, I used [^\\.]+ instead of [^\.]+. This escaping is only because it would result in a JavaScript error if you use an unescaped \ in a JavaScript string. The content of the JavaScript string is then [^\.]+ (only one \). This is not the same escaping as the regular expression escaping above.

Community
  • 1
  • 1
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
0

@ssc-hrep3 almost. The escaping is correct. The test should be the regex Object with the string, not the string with the regex Object. If you change your answer I will mark it as solution.

function escapeRegExp(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

function matchUrl() {
  var currentUrl = window.location.toString();
  var loginUrl =
    escapeRegExp("https://") +
    '[^\\.]+' +
    escapeRegExp(".testsite123.io/testsite123/portal/#/login");
  loginUrl = new RegExp(loginUrl);

  return result = loginUrl.test(currentUrl);
}

if (matchUrl()) {
  //doStuff()
}
mles
  • 4,534
  • 10
  • 54
  • 94