MarksCode MarksCode - 6 months ago 31
Javascript Question

Regex URL no numbers

I want to only execute a function if the main part of the URL is NOT followed by any numbers. For example, I want the following URL to match:

but not

I've gotten together the regex
but it still matches the first part of the links I don't want matched so when I have the statement:

var link = document.URL;
var re = /[^0-9]+/;
if (re.exec(link)){

"hello" still gets logged out. Is there a way to only execute the function if there are no numbers after the main part of the URL even if part of the URL matches?


Use a negative lookahead (?!...)


var link = "";
var re = /link\.com(?!:\d)/;
if (re.exec(link)) {
} else {
  console.log("no match");  

Regex101 Demo