1

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.

9
  • 2
    For a start, if you're using a string to define the regex, you need to escape that backslash. Commented Feb 26, 2015 at 18:08
  • 1
    Try: pattern = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/; Commented Feb 26, 2015 at 18:09
  • @T.J.Crowder I thought that the backslash was escaping the dot... Commented Feb 26, 2015 at 18:09
  • 1
    More importantly: This regex completely fails to handle anything other than the letters A-Z in the English alphabet. We have more expressive TLDs now. I don't think I'd try to validate anything other than that I had at least one character, a dot, and at least one character. And that will, of course, fail for localhost as well, because localhost isn't a domain name. Commented Feb 26, 2015 at 18:10
  • @minseong: It would do in an regex literal. In a string literal, \. 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). Commented Feb 26, 2015 at 18:10

1 Answer 1

3

In your Regex101, you are entering a literal regex, not a string. As your regex is correct (it is) localhost doesn't match. However, in your code, you are creating your regex using strings (bad idea), which means you have to escape each backslash.

You can prevent the issue using Regexp literals.

Just use your original regex with forward-slashes, i.e.

var pattern = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/i;

Or just escape backslashes:

var pattern = new RegExp('^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\\.[a-zA-Z]{2,})+$', 'i');
Sign up to request clarification or add additional context in comments.

6 Comments

I don't want to, I want it to fail. This answer helps me.
antitest 1.c.test.com
(/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/i).test('1.c.test.com') false while it is valid domain name
or more real one: www.w3.org
@eicto I didn't make the regex. I just had to show OP why his code didn't work as they'd expected.
|

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.