10 Regular Expressions that every web developer should know (JavaScript)3 min read

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

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

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

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

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

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

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.

Leave a Comment