I require to validate urls which should be like
google.com or
yahoo.co.uk etc
I mean I dont require http or www. My regex code is like this.But it doesnt work for me.
/^[a-zA-Z0-9][a-zA-Z0-9-][a-zA-Z0-9]\.[a-zA-Z]{2,}$/
I require to validate urls which should be like
google.com or
yahoo.co.uk etc
I mean I dont require http or www. My regex code is like this.But it doesnt work for me.
/^[a-zA-Z0-9][a-zA-Z0-9-][a-zA-Z0-9]\.[a-zA-Z]{2,}$/
Your current regex doesn't work because it expects exactly three characters before the .
: first a character that matches [a-zA-Z0-9]
, then a character that matches [a-zA-Z0-9-]
, then a character that matches [a-zA-Z0-9]
. Your regex only permits a single .
anywhere in the input.
For variable length strings you need to use +
or *
or the {}
syntax like you do for the last part of your regex.
To keep essentially the same validation that you seem to be shooting for but have it work for varying lengths, try:
/^[A-Z\d-]+(\.[A-Z\d-]+)*\.[A-Z]{2,}$/i
That is:
[A-Z\d-]+
match one or more letters, digits or hyphens, followed by
(\.[A-Z\d-]+)*
zero or more instances of a dot followed by one or more of those characters, followed by
\.[A-Z]{2,}
a final dot with two or more of A-Z.
Note that you can get by with just the upper-case A-Z
range if you add the i
flag to the regex to make it case-insensitive.
Your original pattern wants to only allow urls that has the first part not start or end with a dash. In case that is important, I've made a pattern which does that for you.
/^(?:(?!-)[a-z\d-]+[^-]\.)+[a-z]{2,6}$/i
This fixes the same problems that nnnnn solved in his answer, but it also doesn't allow any part of the url to start or end with a hyphen.
I can also recommend regexpal to test matching in real time. It uses javascript style regex matching.
Put the regex into the top field and the test data in the bottom. You will need to remove the slashes encapsulating it. Check the box for "Case insensitive (i)" and "^$ match at line breaks (m)"
Some test data:
google.com
yahoo-.com
www.YAHOO.co.uk
-yahoo.co.uk
http://www.regular-expressions.info/
hey.subdomain.sub.sub.domain.com
co.uk
Now, the difference between using ^$ or not will prove itself. You will see that some urls are not matching, even though they are valid. The multiline (m) flag allows you to think of each line in the input as a separate string. The regex I gave you will only match if the entire string matches, but some urls are valid even so. Case insensitive (i) equals the "i" at the end of the regex.
Try to remove the anchors for start of line (^) and end of line ($), and see how that matches.
I'm not sure how you are running your matches, but this is worth considering.
Try this:
(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}