Interlude: Common tasks

Tasks like matching phone numbers, ip addresses, dates, etc are so common that you can often find them collected as a library. This chapter shows some examples for CommonRegexJS. See also Awesome Regex: Collections.

CommonRegexJS

There are several ways to use the patterns, see CommonRegexJS: Usage for details. You can also go through commonregex.js and choose the regular expression you need.

> let data = 'hello 255.21.255.22 okay'

> const comm = new CommonRegex(data)

> comm.IPv4
< ["255.21.255.22"]

Make sure to test these patterns for your use case. For example, the below data has a valid IPv4 address followed by another number separated by a dot character. If such cases should be ignored, then you'll have to create your own version of the pattern or change the input accordingly.

> let new_data = '23.14.2.4.2 255.21.255.22 567.12.2.1'

> const ip = new CommonRegex(new_data)

> ip.IPv4
< ["23.14.2.4", "255.21.255.22"]

Summary

Some patterns are quite complex and not easy to build and validate from scratch. Libraries like CommonRegexJS are helpful to reduce your time and effort needed for commonly known tasks. However, you do need to test the solution for your use case. See also stackoverflow: validating email addresses.