javascript - Regex for valid url -


i require validate urls should like

google.com  or yahoo.co.uk etc 

i mean dont require http or www. regex code this.but doesnt work me.

/^[a-za-z0-9][a-za-z0-9-][a-za-z0-9]\.[a-za-z]{2,}$/ 

your current regex doesn't work because expects 3 characters before .: first character matches [a-za-z0-9], character matches [a-za-z0-9-], character matches [a-za-z0-9]. regex permits single . anywhere in input.

for variable length strings need use + or * or {} syntax last part of regex.

to keep same validation seem shooting have work varying lengths, try:

/^[a-z\d-]+(\.[a-z\d-]+)*\.[a-z]{2,}$/i 

that is:

[a-z\d-]+ match 1 or more letters, digits or hyphens, followed by

(\.[a-z\d-]+)* 0 or more instances of dot followed 1 or more of characters, followed by

\.[a-z]{2,} final dot 2 or more of a-z.

note can upper-case a-z range if add i flag regex make case-insensitive.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -