I want to create a regular expression to verify domain names.
I've searched a lot on StackOverflow and I found this solution, which ignores the http and folders and checks only for domain.
QUOTE
var pattern = new RegExp('^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$', 'i');
But since I'm not a wizard and I've long ago given up on trying to understand Regex, I placed it on Regex101 which shows me that 'localhost' is not a match. The problem is when I execute the following code (jsFiddle here):
var origin = 'localhost';
var pattern = new RegExp('^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$', 'i');
console.log(origin, pattern.test(origin));
It returns true (localhost should not be matched). Why?
Edit: For some reason, people thought I WANTED localhost to be recognized as a domain. BUT IT'S THE OPPOSITE! If you read the question, you'll see that it's returning true although it should return false.
pattern = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/;localhostas well, becauselocalhostisn't a domain name.\.is.(because the backslash is an escape in the string literal that escapes the dot, which doesn't need escaping, and ends up doing thing; the regex engine never sees that backslash).