Amazing Regex Code Snippets for Web Developers

If you consider yourself a programmer and you have not learned regular expressions yet now is the time! At some point in your development you will need to manipulate strings in more complicated ways that simple string functions can’t provide. Regular expressions are used in almost all programming languages and are considered the defacto standard for string manipulation. Regular expressions can also be used in searches and many other utilities.

Form Validation

Password Strength

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

A good starting point for creating a password strength checker. Everyone has different ideas about what makes for a strong password, but allows you to not have to make a validator from scratch.

Validate Email Address

/[A-Z0-9._%+-]+@[A-Z0-9-]+.+.[A-Z]{2,4}/igm

Whenever you deal with user and newsletter signups, you have to deal with email validation. In order to keep spam at a minimum, this is an absolute must.

Pull Domain from URL

/https?:\/\/(?:[-\w]+\.)?([-\w]+)\.\w+(?:\.\w+)?\/?.*/i

To validate where links are coming from you can use this to pull the domain name from any URL.

Valid Phone Number

^\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d$

This works only with American phone number, as phone number validation can be a very tricky subject due to different standard across the globe.

Validate Date in DD/MM/YYYY Format

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

With a large variety of Date formats this helps with the American standard format.

YouTube Video ID Match

/http:\/\/(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#\!)v=)([\w-]{11}).*/gi

Check if the YouTube URL is valid and pull the ID.

Check Zip Code

^\d{5}(?:[-\s]\d{4})?$

This code can validate a 5 digit zip code, with or without the 4 digit extension.

Valid Twitter Username

/@([A-Za-z0-9_]{1,15})/

Can be used for any alphanumeric username that allows for underscores.

Credit Card Numbers

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$

Definitely not a Luhn test, but checks for basic card number formatting.

Facebook Profile URL

/(?:http:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-]*)/

Checks if URL has the proper format for a Facebook user or page.

Find Particular Filetype

/^(.*\.(?!(htm|html|class|js)$))?[^.]*$/i

Can be manipulated to check for different filetypes on uploaded files or in any URL.

Text Formatting

Thousands Separator

/\d{1,3}(?=(\d{3})+(?!\d))/g

Show numbers with the proper formatting (e.g. 1,000,000).

Extract Price

/(\$[0-9,]+(\.[0-9]{2})?)/

Pull price from any string.

Append rel=”nofollow” to Links

(<a\s*(?!.*\brel=)[^>]*)(href="https?://)((?!(?:(?:www\.)?'.implode('|(?:www\.)?', $follow_list).'))[^"]+)"((?!.*\brel=)[^>]*)(?:[^>]*)>

Decide whether or not to ad nofollow to any links displayed you your site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s