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

5
  • 1
    use RegExp literals to avoid all the confusing escaping problems Commented Mar 25, 2017 at 23:43
  • When you say "specific url" at what point did * become anything close to specific? https://*.??? Commented Mar 25, 2017 at 23:52
  • @dandavis My URL has / and # so I don't know with any other literals I should escape it. Commented Mar 26, 2017 at 0:01
  • @zer00ne sorry I meant specific url with random subdomain. the subdomain is the only thing that changes. I've updated the question. Commented Mar 26, 2017 at 0: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 . Commented Mar 26, 2017 at 0:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.