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

/^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].\*[a-z]).{8}$/

Breakdown:

/     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

/^-?\d+$/

3. Validate an email address

/^([a-z0-9\_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

4. Validate IPv4 addresses

/^(?!0)(?!.\*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/

5. Validate IPv6 addresses

/^(([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])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){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-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.

/^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/

9. Check a Username

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

/^[\w\d\_.]{4,}$/

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.

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

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! Thanks for reading :P I just want to let you know that if you enjoyed this, you might also like this post on Sorting.

ABOUT THE AUTHOR RSS Feed Icon

I'm Bobby Yan, a programmer, music lover and web developer. I am currently working on IB Notes, a website with useful and concise study guides and notes for IB students. I have also built the Spanish Vocabulary Builder iOS app to help Spanish learners.

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: