October 9, 2013 362 Views 3 Comments

Regular Expressions (RegEx) – Quick Reference


Regular expression also known as regex is a sequence of characters/symbols that forms a search pattern. It is supported by most programming language including Perl, Javascript, PHP, C-Sharp and Java.

Commonly Used Symbols and Syntax

Start of subject (or line in multiline mode)
End of subject (or line in multiline mode)
Start character class definition
End character class definition
Alternates ( a|b matches a or b )
Start subpattern
End subpattern
Escape character
Any “word” character (a-z 0-9 _)
Any non-“word” character
Whitespace (space, tab, CRLF)
Any non-whitespace character
Digits (0-9)
Any non-digit character
Any character except newline
Zero or more
One or more
Zero or one occurrence
n occurrences exactly
At least n occurrences
At most m occurrences
Between n and m occurrences inclusive
list or range of characters (or both). [abc] means “any single character that is either a, b or c”
any single character that is not in the class. [^0-9xyz] matches any single character that isn’t a digit and isn’t the letter x, y, or z
escape sequences r (carriage return)
escape sequences t (tab)

These are most of the commonly-used RegEx features, there are quite a few others you may want to explore. The complete manual is at www.pcre.org/pcre.txt


Here’s a list of some useful regular expressions that can make life as a developer less hassling.

Regular expression for username

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


Regular expression for email


Regular expression for Password

Password must contain letters a-zA-Z and at least one digit 0-9



(/^(?=.*d) //should contain at least one digit
(?=.*[a-z]) //should contain at least one lower case
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{8,} //should contain at least 8 from the mentioned characters $/)

Regular expression for URL


Regular expression for Decimal value


Regular expression for Float value


Regular expression for Removing Whitespaces

str.replace(/^s+|s+$|s+(?=s)/g, "")

Share your favorite RegEX using the comment form below…

  • Sukey

    Thanks for the password guidance here. We’ve got a client asking to improve the password security on the site through stronger passwords and I was stumbling a bit in terms of getting the syntax for a clean run. This is working perfectly!

  • John

    Getting better password security is very important! I have been working all morning on trying to figure out how to complete the code. Your tips helped me a great deal, thanks!

    • Berry

      Password security is very important, I agree, but when coding a site you should realize that you cannot control everything the user does. They should provide good, long, complex passwords to begin with.