10 Regular Expressions that every web developer should know

Follow by Email
The hardest part is learning the syntax and learning how to write your own regex code from scratch. To save time, I have selected 30 different regex code snippets that you can use in your projects. And since regex is not limited to a particular language, you can apply the following paragraphs in any language from Javascript to PHP or Python.
1 The strength of the password

^(?=.*[A-Z].*[A-Z])(?=.*[[email protected]#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
Testing the strength of a password is often subjective so there is no absolute exact answer. But I feel that this regex is a great starting point if you don’t want to write your own password strength checker from scratch.
2 Hex Color Code

\#([a-fA-F]|[0-9]){3, 6}
Hex color codes are very popular in the field of web development. This regex fragment can be used to retrieve the appropriate hex code from any string for any purpose.

3 Authentication email address
/[A-Z0-9._%+-][email protected][A-Z0-9-]+.+.[A-Z]{2,4}/igm
One of the most common tasks for a developer is to check whether a string is a valid email address or not. For PHP, you can use the function filter_var: filter_var ($ email, FILTER_VALIDATE_EMAIL)
4 Address IPv4
Similar to an email address is the IP address – is used to identify a specific computer to access the Internet. This regular expression will check a string if it complies with the syntax of IPv4 addresses or not.
5 Address IPv6
Or maybe you’ll want to check out an IPv6 address advanced regex syntax. The difference is very small, although it is important in the development process.
6 Thousands separator
This regex code works on any number and will apply any delimiter for each 3 digits separated by thousands, millions, …
7 Add HTTP before the link
    if (!s.match(/^[a-zA-Z]+:\/\//))
        s = 'http://' + s;
Whether you’re working in JavaScript, Ruby or PHP, this expression could be very useful. It will check any URL string to see if it has the prefix HTTP / HTTPS or not, and if not, add this string to match.
8 Get domain from URL
Each domain name contains the site in the protocol (HTTP or HTTPS) and sometimes the path, subdomain with additional pages. You can use this regex segments to cut through all of that and just return the domain name.
9 Sorting keywords by counting the number of words
^[^\s]*$        matches exactly 1-word keyword
^[^\s]*\s[^\s]*$    matches exactly 2-word keyword
^[^\s]*\s[^\s]*     matches keywords of at least 2 words (2 and more)
^([^\s]*\s){2}[^\s]*$    matches exactly 3-word keyword
^([^\s]*\s){4}[^\s]*$    matches 5-words-and-more keywords (longtail)
Those who use Google Analytics and Webmaster Tools will really like this regular expression. It can sort the keywords based on the words use searching.
This may be a specific amount (ie only 5 words) or it can fit a variety of words (ie 2 or more). When used to sort and analyze the data, it is one of the most powerful regular expressions.
10 Find a valid Base64 string in PHP
\?php[ \t]eval\(base64_decode\(\'(([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?){1}\'\)\)\;
If you’re a PHP developer, then sometimes you can analyze through the code to find objects Base64 encoded binary (eg looking shell hidden in a PHP). This regex segments can apply to all PHP code and will examine any of the existing Base64 string.
Take your time to comment on this article.
Follow by Email


  1. json editor 2017-07-04 Reply
  2. Mike 2017-09-26 Reply
  3. rayjay 2017-10-16 Reply
  4. Waseem Hilal 2017-10-22 Reply
  5. Ed 2017-11-11 Reply
  6. bullet force 2017-12-09 Reply
  7. gmail login 2018-01-09 Reply

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger