Home of Bobby Yan

A site all about me

I am Bobby Yan, an enthusiastic maker of things. Call me a programmer, web developer, designer or a teacher. Currently a freshman at UC Berkeley studying Electrical Engineering and Computer Science.

Check out my bio!

12 Regular Expressions that are Good to Know

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.

Recent Posts

An Introduction to Markdown

Getting to know MarkdownMarkdown is a lightweight markup language with many advantages, and it’s growing in popularity among writers, editors, etc. Now, don’...…

Older Posts

How to view documents without downloading (almost instantly)

This is a quick and sleek tutorial on how to view documents in browser without downloading the files to your computer. This is especially handy for larger fi...…