5/28/2017

10 Regular Expressions that every web developer should know

Image result for image of a hackers keyboard

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
 
 class="hljs language-JavaScript">^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[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
 
 class="hljs language-JavaScript">\#([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
 
 class="hljs language-JavaScript">/[A-Z0 class="hljs-number">-9._%+-]+@[A-Z0 class="hljs-number">-9-]+.+.[A-Z]{ class="hljs-number">2, class="hljs-number">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
 
 class="hljs language-JavaScript">/\b(?:(?: class="hljs-number">25[ class="hljs-number">0 class="hljs-number">-5]| class="hljs-number">2[ class="hljs-number">0 class="hljs-number">-4][ class="hljs-number">0 class="hljs-number">-9]|[ class="hljs-number">01]?[ class="hljs-number">0 class="hljs-number">-9][ class="hljs-number">0 class="hljs-number">-9]?)\.){ class="hljs-number">3}(?: class="hljs-number">25[ class="hljs-number">0 class="hljs-number">-5]| class="hljs-number">2[ class="hljs-number">0 class="hljs-number">-4][ class="hljs-number">0 class="hljs-number">-9]|[ class="hljs-number">01]?[ class="hljs-number">0 class="hljs-number">-9][ class="hljs-number">0 class="hljs-number">-9]?)\b/
 
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
 
 class="hljs language-JavaScript">(([ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:){ class="hljs-number">7, class="hljs-number">7}[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}|([ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:){ class="hljs-number">1, class="hljs-number">7}:|([ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:){ class="hljs-number">1, class="hljs-number">6}:[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}|([ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:){ class="hljs-number">1, class="hljs-number">5}(:[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}){ class="hljs-number">1, class="hljs-number">2}|([ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:){ class="hljs-number">1, class="hljs-number">4}(:[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}){ class="hljs-number">1, class="hljs-number">3}|([ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:){ class="hljs-number">1, class="hljs-number">3}(:[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}){ class="hljs-number">1, class="hljs-number">4}|([ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:){ class="hljs-number">1, class="hljs-number">2}(:[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}){ class="hljs-number">1, class="hljs-number">5}|[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:((:[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}){ class="hljs-number">1, class="hljs-number">6})|:((:[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}){ class="hljs-number">1, class="hljs-number">7}|:)|fe80:(:[ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">0, class="hljs-number">4}){ class="hljs-number">0, class="hljs-number">4}%[ class="hljs-number">0 class="hljs-number">-9a-zA-Z]{ class="hljs-number">1,}|::(ffff(: class="hljs-number">0{ class="hljs-number">1, class="hljs-number">4}){ class="hljs-number">0, class="hljs-number">1}:){ class="hljs-number">0, class="hljs-number">1}(( class="hljs-number">25[ class="hljs-number">0 class="hljs-number">-5]|( class="hljs-number">2[ class="hljs-number">0 class="hljs-number">-4]| class="hljs-number">1{ class="hljs-number">0, class="hljs-number">1}[ class="hljs-number">0 class="hljs-number">-9]){ class="hljs-number">0, class="hljs-number">1}[ class="hljs-number">0 class="hljs-number">-9])\.){ class="hljs-number">3, class="hljs-number">3}( class="hljs-number">25[ class="hljs-number">0 class="hljs-number">-5]|( class="hljs-number">2[ class="hljs-number">0 class="hljs-number">-4]| class="hljs-number">1{ class="hljs-number">0, class="hljs-number">1}[ class="hljs-number">0 class="hljs-number">-9]){ class="hljs-number">0, class="hljs-number">1}[ class="hljs-number">0 class="hljs-number">-9])|([ class="hljs-number">0 class="hljs-number">-9a-fA-F]{ class="hljs-number">1, class="hljs-number">4}:){ class="hljs-number">1, class="hljs-number">4}:(( class="hljs-number">25[ class="hljs-number">0 class="hljs-number">-5]|( class="hljs-number">2[ class="hljs-number">0 class="hljs-number">-4]| class="hljs-number">1{ class="hljs-number">0, class="hljs-number">1}[ class="hljs-number">0 class="hljs-number">-9]){ class="hljs-number">0, class="hljs-number">1}[ class="hljs-number">0 class="hljs-number">-9])\.){ class="hljs-number">3, class="hljs-number">3}( class="hljs-number">25[ class="hljs-number">0 class="hljs-number">-5]|( class="hljs-number">2[ class="hljs-number">0 class="hljs-number">-4]| class="hljs-number">1{ class="hljs-number">0, class="hljs-number">1}[ class="hljs-number">0 class="hljs-number">-9]){ class="hljs-number">0, class="hljs-number">1}[ class="hljs-number">0 class="hljs-number">-9]))
 
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
 
 class="hljs language-JavaScript">/\d{ class="hljs-number">1, class="hljs-number">3}(?=(\d{ class="hljs-number">3})+(?!\d))/g
 
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
 
 class="hljs language-JavaScript">     class="hljs-keyword">if (!s.match( class="hljs-regexp">/^[a-zA-Z]+:\/\//))
    {
        s =  class="hljs-string">'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
 
 class="hljs language-JavaScript">/https?:\/\/(?:[-\w]+\.)?([-\w]+)\.\w+(?:\.\w+)?\/?.*/i
 
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
 
 class="hljs language-JavaScript">^[^\s]*$        matches exactly  class="hljs-number">1-word keyword
^[^\s]*\s[^\s]*$    matches exactly  class="hljs-number">2-word keyword
^[^\s]*\s[^\s]*     matches keywords  class="hljs-keyword">of at least  class="hljs-number">2 words ( class="hljs-number">2 and more)
^([^\s]*\s){ class="hljs-number">2}[^\s]*$    matches exactly  class="hljs-number">3-word keyword
^([^\s]*\s){ class="hljs-number">4}[^\s]*$    matches  class="hljs-number">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
 
 class="hljs language-JavaScript">\?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.