In programming, there are many ways to work with strings. A performant way to work with them is through the use of Regular expressions, also known as Regex.
Regular expressions are difficult to understand for new developers. Even for experienced developers, it can become difficult to work with regex. This tutorial will hopefully clear up any complications with regex and get you started working with regular expressions in your own code.
Instantiating a new RegExp object
const regex1 = new RegExp('Hello World')
Using the literal form
const regex1 = /Hello World/
In the first example, ‘Hello World’ is a pattern. In this second example, the pattern is delimited by the forward slashes, while there are no forward slashes with the object constructor.
How regular expressions work?
Earlier, I mentioned, regular expressions can be used for searching a substring within a string. This is one basic use of a regular expression. Take a look at the following RegExp object.
const regex1 = RegExp('Hello World')
What does this actually mean? It searches the string ‘Hello World’ without any limitations. It does not matter if another string contains ‘Hello World’ in the middle or starting, or at the end, or even if the string contains only ‘Hello World’, the regex is satisfied. It does not matter how long the text is.
We can use the test() method to test the regex. This method returns a boolean value.
regex1.('sdadasd Hello World asdsadadas') // returns true regex1.('Hello World asdsadadas') // returns true regex1.('sdadasd Hello World') // returns true
The above three will return true because the substring is present in each of the string passes. Now, have a look at the following.
regex1.('sdadasd World asdsadadas') // returns false regex1.('Hello asdsadadas') // returns false regex1.('sdadasd World') // returns false
This time, all three will return false because none of the above contains the exact substring.
This is a simple way of using regular expressions.
Matching is another benefit of regular expressions. Suppose we need to find if a string contains any alphabetic characters. We can use the following method.
/[a-z]/.test('abc232') // returns true
a-z matches against all or any characters between a and z. When this range is specified between square brackets, the regex will search for any character in a given string. As the string passed(‘abc232’), contains at least one alphabetic character, the test() method returns true.
Similarly, we have a few other ranges.
/[a-z]/ // a,b,c,d,e.....x,y,z /[A-Z]/ // A,B,C,D,E.....X,Y,Z /[0-9]/ // 0,1,2,3,4.....7,8,9
We can also combine these ranges.
Let’s see a few more examples.
/[a-d]/ // returns true if a or b or c or d is present in the string /[0-2]/ // returns true if 0 or 1 or 2 is present in the string
There is also a wide range of metacharacters.
- w – matches any alphanumeric character plus underscores. It is equivalent to [A-Za-z_0-9].
- W – matches anything except [A-Za-z_0-9]
- d – equivalent to [0-9]
- D – matches anything except [0-9]
- s – matches any whitespace character
- S – matches any character that is not a whitespace
- n – matches a newline