Reputation: 65
I wrote a regular expression (https?:\/\/)+([a-x]*)?.[a-z]*.(com|io|cn|net)
that can achieve:
but it also considered 'http://ww#.123.com' as the correct answer, I wonder what's wrong with my expression, how to limit input '#'.
Upvotes: 1
Views: 69
Reputation: 2293
If you use a RegEx tester online (like regex101.com) it will tell you that it's matching because the .
is not escaped as \.
so it will match the #
character.
Try: ^(https?:\/\/)([a-z0-9_]*\.)?[a-z0-9_]*\.(com|io|cn|net)$
and you may get what you're looking for.
Note your original RegEx did not include digits or the underscore in the domain names.
Upvotes: 2