The regular expression also called regex is a way to work with strings, in a very performant way.
By formulating the regular expression with a special syntax, you can perform the following operations.
- Search text in a string.
- Replace substrings in a string.
- Extract information from a string.
#Create a Node.js project.
Create one project directory using the following command.
Now, inside the root of the project, create one file called the server.js and add the following code.
// server.js const str = 'let us browse appdividend.com'; const l = str.search("appdividend"); console.log(l); // 14
Here, what we have done is looking for the starting index of the provided string. In our case it is appdividend, so it will return the starting position of the appdividend which is 14.
The search() method searches the string for a specified value and returns the position of the match.
We can also search the string with a Regular Expression. Let us see the following example.
// server.js const str = 'let us browse appdividend.com'; const l = str.search(/appdividend/); console.log(l); // 14
In the above case also, we will get the same output.
#String replace() With a String
// server.js const str = 'let us browse appdividend.com'; const replaced = str.replace('appdividend', 'youtube'); console.log(replaced);
Here, you will get the output like let us browse youtube.com.
You can also do the same replace operation using the Regular Expression.
// server.js const str = 'let us browse appdividend.com'; const replaced = str.replace(/appdividend/, 'youtube'); console.log(replaced);
#Regular Expression Patterns
|[abc]||Find any of the characters between the brackets|
|[0-9]||Find any of the digits between the brackets|
|(x|y)||Find any of the alternatives separated with ||
Metacharacters are characters with a special meaning:
|\d||Find a digit|
|\s||Find a whitespace character|
|\b||Find a match at the beginning of the end of a word|
|\uxxxx||Find a Unicode character specified by a hexadecimal number xxxx|
#Define Regular Expression
The first is by instantiating the new RegExp object using a constructor:
// server.js const re = new RegExp('appdividend'); console.log(re); // /appdividend/
The second is using a regular expression literal form:
// server.js const re = /appdividend/;
In the example above, the appdividend is called the pattern. In the literal form it’s delimited by the forward slashes, while with the object constructor, it’s not.
#Using test() method
You can test a regex using RegExp.test(), which returns the boolean:
A test() method is the RegExp expression method.
It searches the string for the pattern, and returns true or false, depending on a result.
The following example searches the string for the character “appdividend“:
// server.js const re = /appdividend/; console.log(re.test('you can browse appdividend')); // true
#Using exec() method
An exec() method is the RegExp expression method.
It searches the string for a specified pattern and returns a found text.
If no match is found, then it returns null.
// server.js const re = /appdividend/; console.log(re.exec('you can browse appdividend'));
You will get an output like this.
#Match Items In Range
Instead of matching the particular string, you can choose to match any character in a range. Let us take an example and understand.
// server.js const re = /[a-z]/; console.log(re.test('appdividend')); // true
But the following give you falsely result.
// server.js const re = /[a-z]/; console.log(re.test('APPDIVIDEND')); // false
Also, the regular expressions can be combined so that we can make it above example true.
// server.js const re = /[A-Za-z0-9]/ console.log(re.test('APP10app')); // true