10 Regular Expressions that every web developer should know

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/05/10-regular-expressions-every-web-developer-know/
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

<code class="hljs language-JavaScript">^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&amp;*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$</code>
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

<code class="hljs language-JavaScript">\#([a-fA-F]|[0-9]){3, 6}</code>
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
<code class="hljs language-JavaScript">/[A-Z0<span class="hljs-number">-9.</span>_%+-]+@[A-Z0<span class="hljs-number">-9</span>-]+.+.[A-Z]{<span class="hljs-number">2</span>,<span class="hljs-number">4</span>}/igm
</code>
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
<code class="hljs language-JavaScript">/\b(?:(?:<span class="hljs-number">25</span>[<span class="hljs-number">0</span><span class="hljs-number">-5</span>]|<span class="hljs-number">2</span>[<span class="hljs-number">0</span><span class="hljs-number">-4</span>][<span class="hljs-number">0</span><span class="hljs-number">-9</span>]|[<span class="hljs-number">01</span>]?[<span class="hljs-number">0</span><span class="hljs-number">-9</span>][<span class="hljs-number">0</span><span class="hljs-number">-9</span>]?)\.){<span class="hljs-number">3</span>}(?:<span class="hljs-number">25</span>[<span class="hljs-number">0</span><span class="hljs-number">-5</span>]|<span class="hljs-number">2</span>[<span class="hljs-number">0</span><span class="hljs-number">-4</span>][<span class="hljs-number">0</span><span class="hljs-number">-9</span>]|[<span class="hljs-number">01</span>]?[<span class="hljs-number">0</span><span class="hljs-number">-9</span>][<span class="hljs-number">0</span><span class="hljs-number">-9</span>]?)\b/
</code>
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
<code class="hljs language-JavaScript">(([<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">7</span>,<span class="hljs-number">7</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}|([<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">7</span>}:|([<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">6</span>}:[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}|([<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">5</span>}(:[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">2</span>}|([<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}(:[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">3</span>}|([<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">3</span>}(:[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}|([<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">2</span>}(:[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">5</span>}|[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:((:[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">6</span>})|:((:[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">7</span>}|:)|fe80:(:[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">0</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">0</span>,<span class="hljs-number">4</span>}%[<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-zA-Z]{<span class="hljs-number">1</span>,}|::(ffff(:<span class="hljs-number">0</span>{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}:){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}((<span class="hljs-number">25</span>[<span class="hljs-number">0</span><span class="hljs-number">-5</span>]|(<span class="hljs-number">2</span>[<span class="hljs-number">0</span><span class="hljs-number">-4</span>]|<span class="hljs-number">1</span>{<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>]){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>])\.){<span class="hljs-number">3</span>,<span class="hljs-number">3</span>}(<span class="hljs-number">25</span>[<span class="hljs-number">0</span><span class="hljs-number">-5</span>]|(<span class="hljs-number">2</span>[<span class="hljs-number">0</span><span class="hljs-number">-4</span>]|<span class="hljs-number">1</span>{<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>]){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>])|([<span class="hljs-number">0</span><span class="hljs-number">-9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:((<span class="hljs-number">25</span>[<span class="hljs-number">0</span><span class="hljs-number">-5</span>]|(<span class="hljs-number">2</span>[<span class="hljs-number">0</span><span class="hljs-number">-4</span>]|<span class="hljs-number">1</span>{<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>]){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>])\.){<span class="hljs-number">3</span>,<span class="hljs-number">3</span>}(<span class="hljs-number">25</span>[<span class="hljs-number">0</span><span class="hljs-number">-5</span>]|(<span class="hljs-number">2</span>[<span class="hljs-number">0</span><span class="hljs-number">-4</span>]|<span class="hljs-number">1</span>{<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>]){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span><span class="hljs-number">-9</span>]))
</code>
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
<code class="hljs language-JavaScript">/\d{<span class="hljs-number">1</span>,<span class="hljs-number">3</span>}(?=(\d{<span class="hljs-number">3</span>})+(?!\d))/g
</code>
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
<code class="hljs language-JavaScript">    <span class="hljs-keyword">if</span> (!s.match(<span class="hljs-regexp">/^[a-zA-Z]+:\/\//</span>))
    {
        s = <span class="hljs-string">'http://'</span> + s;
    }
</code>
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
<code class="hljs language-JavaScript">/https?:\/\/(?:[-\w]+\.)?([-\w]+)\.\w+(?:\.\w+)?\/?.*/i
</code>
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
<code class="hljs language-JavaScript">^[^\s]*$        matches exactly <span class="hljs-number">1</span>-word keyword
^[^\s]*\s[^\s]*$    matches exactly <span class="hljs-number">2</span>-word keyword
^[^\s]*\s[^\s]*     matches keywords <span class="hljs-keyword">of</span> at least <span class="hljs-number">2</span> words (<span class="hljs-number">2</span> and more)
^([^\s]*\s){<span class="hljs-number">2</span>}[^\s]*$    matches exactly <span class="hljs-number">3</span>-word keyword
^([^\s]*\s){<span class="hljs-number">4</span>}[^\s]*$    matches <span class="hljs-number">5</span>-words-and-more keywords (longtail)
</code>
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
<code 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}\'\)\)\;
</code>
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
Facebook0
Facebook
Google+
https://codingsec.net/2017/05/10-regular-expressions-every-web-developer-know/

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