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 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


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


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


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

YouTube Video ID Match


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

Check Zip Code


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

Valid Twitter Username


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

Credit Card Numbers


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

Facebook Profile URL


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

Find Particular Filetype


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

Text Formatting

Thousands Separator


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

Extract Price


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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s