8 Most useful regular expressions

Regular expression also known as regex or regexp is a sequence of characters that forms a search pattern. It is used by most of the programming language like php, javascript, perl etc.

Here is the list of most useful regular expressions for webmasters. To test them on text string use our Regular expression testing tool.

Regular expression for username

Following pattern will match alphanumeric string including “_” of length 3 to 16.

^[a-z0-9_]{3,16}$

Regular expression for email

^[a-z0-9]+[_a-z0-9.-]*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$

Regular expression for URL

^http[s]?://[a-z0-9-.]+.[a-z.]{2,5}(/[a-z0-9%-_.?=&@#]*)?$

Regular expression for Domain Name

^[a-z0-9]+[a-z0-9-.]*[a-z0-9]+.[a-z.]{2,5}$

Regular expression for IP-address

This will match any IP-address between 0.0.0.0 to 255.255.255.255

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

Regular expression for Decimal value

Following pattern will match any decimal digit of any size.

^[0-9]+$

Regular expression for Float value

This will match any floating point value eg 1.2, 2.009 or .987 but not match 1.00 or 239

^[0-9]*.[0-9]*[1-9]+$

Regular expression for Hexadecimal color

This Regex pattern will match any 3 or 6 digit hexadecimal value eg: FF2 or 33CCEE

^([a-f0-9]{6}|[a-f0-9]{3})$

Published by Sunny

A Mechanical engineer by education and web developer by passion and profession. He lives in Lucknow, India. Currently earning his livings by hitting keyboad. ;)

Leave a comment

Your email address will not be published. Required fields are marked *