A regular expression, abbreviated regex or regexp, is a very old and powerful way to describe search patterns by using a concise text string to define complex searching algorithms. As a programmer, if you can master regular expressions, you can save yourself a whole lot of time and effort. One regular expression can often accomplish what would otherwise take tens or hundreds of code.

Regular expressions are frequently used to check for the validity of text strings of any sequence of characters. Here is what a typical regex to validate date format looks like in Javascript:

function validateDate(testdate) {
	var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
	return date_regex.test(testdate);

You’re probably wondering why you couldn’t just, say, use Date.Parse() in Javascript. That works well until you try to mess around with the UK date format (dd/mm/yyyy).

Now that we know how useful regular expressions are, I want to just list a few handy ones.

1. Test password strength



/     Delimiter
^     Start anchor
(?=.*[A-Z].*[A-Z])     Ensure string has two uppercase letters
(?=.\*[[email protected]#$&\*])     Ensure string has one special case letter
(?=.*[0-9].*[0-9])     Ensure string has two digits
(?=.*[a-z].*[a-z].\*[a-z])     Ensure string has three lowercase letters
.{8}     Ensure string is of length 8
$     End anchor

2. Matching an integer


3. Validate an email address


4. Validate IPv4 addresses


5. Validate IPv6 addresses


6. Check a Hex Value


7. Check a URL

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

8. Check if a string is currency

This regex works like isDecimal, except that only zero or two digits are allowed after the decimal points.


9. Check a Username

Checks a username to see if it has at least 4 characters.


10. Check IE version

Though obsolete, Internet Explorer hasn’t been completely replaced yet, so many sites may still consider fixing their compatibility issues with IE. Here’s a regex to check for IE version.

/^.*MSIE [5-8][1]?(?!.*Trident\\/[5-9]\\.0).\*$/

11. Check the URL Prefix

There’re plenty of times in web development when you need to check the URL prefix, whether it’s http:// or https://, and you can do that with this regex:

if ( !s.match( /^[a-zA-Z]+:\\/\\// ) ) {
	s = 'http://' + s;

12. Check a Domain

This checks if a domain name is valid.


I found a really awesome quick reference guide for regular expressions, and a pretty good cheat sheet for Javascript regular expressions here: Javascript regex cheat sheet

Also, Regex101 is an excellent regular expressions tester that highlights pattern and matches online.

Hey, kudos for making it this far! Wanted to let you know that if you liked this, you might also like tmux Cheatsheet and Shortcuts.


I'm Bobby Yan, a programmer, music lover, researcher, and a third-year undergraduate student at UC Berkeley majoring in Electrical Engineering and Computer Science. I am currently doing research on robotics, planning, and systems for machine learning at RISELab. In the past, I built IB Notes, a website with useful and concise study guides and notes for IB, and Spanish Vocab Builder, an iOS app to help Spanish learners. See my résumé for more details.

If you enjoyed this article, you should follow me on Twitter or sign up to get an email whenever I write something new:

Share this article with your friends: