Developer tools: Match for Site url

I am using Dev tools in Google chrome and need to find out is the site url contains a specific string. I am able to get Url_Str correctly, although when I run the match command it always returns a match found even when it is not valid . How do I correct this?

let Url_Str = ___grecaptcha_cfg.clients[0].B.S.baseURI;
if (Url_Str.match(/*myconstant*/)) {"match found";}

Also is there a Alternate way to get the site url instead of using grecaptcha_cfg?

you can get URL using window.location.href

window.location.host: “forum.freecodecamp.org
window.location.hostname: “forum.freecodecamp.org
window.location.href: https:/ /forum.freecodecamp.org/c/javascript/421
window.location.origin: “https://forum.freecodecamp.org

1 Like

Thanks for that. Any ideas how to fix my initial issue? Basically I need to find out if the line (url) contains a specific keyword.

yes, what word are you looking to find?


const c = window.location.href
const regex =/forum|com|site/gi; //add more than one word after |
regex.test(c)  //true - u can use one

const found = c.match(regex); //["forum", "site"]

1 Like

Thanks Qasim for your help. That was exactly what I needed.

let Url_Str = window.location.href;
let a = 0
if(/myconstant/.test(Url_Str) === true) {a = 1;}
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.