MarksCode MarksCode - 1 year ago 85
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?

Answer Source

Use a negative lookahead (?!...)


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

Regex101 Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download