Regular expressions is a powerful tool in any language. Here I am discussing how to use regular expression in JavaScript.
Defining regular expressions
In JavaScript regular expression can be defined two ways.
1 2 | |
If I am defining regular expression using “RegExp”:https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp#Properties_2 then I need to add escape character in certain cases.
1 2 | |
When I am defining regular expression using RegExp then \w needs to be escaped otherwise it would be taken literally.
test method
test method is to check if a match is found or not. This method returns true or false.
1 2 3 | |
exec method
exec method finds if a match is found or not. It returns an array if a match is found. Otherwise it returns null.
1 2 3 4 | |
match method
match method acts exactly like exec method if no g parameter is passed. When global flag is turned on the match returns an Array containing all the matches.
Note that in exec the syntax was regex.exec(text) while in match method the syntax is text.match(regex) .
1 2 3 4 | |
Now with global flag turned on.
1 2 3 4 | |
Getting multiple matches
Once again both exec and match method without g option do not get all the matching values from a string. If you want all the matching values then you need to iterate through the text. Here is an example.
Get both the bug numbers in the following case.
1 2 3 4 5 6 7 | |
Note that in the above case global flag g. Without that above code will run forever.
1 2 3 4 5 | |
In the above case match is used instead of regex . However since match with global flag option brings all the matches there was no need to iterate in a loop.
match attributes
When a match is made then an array is returned. That array has two methods.
- index: This tells where in the string match was done
- input: the original string
1 2 3 4 5 | |
replace
replace method takes both regexp and string as argument.
1 2 3 | |
Example of using a function to replace text.
1 2 3 | |
Another case.
1 2 3 4 | |
Example of using special variables.
- $& - the matched substring.
- $` - the portion of the string that precedes the matched substring.
- $’ - the portion of the string that follows the matched substring.
- $n - $0, $1, $2 etc where number means the captured group.
1 2 3 4 | |
1 2 3 4 | |
Replace method also accepts caputured groups as parameters in the function. Here is an example;
1 2 3 4 5 6 7 8 | |
As you can see the very first argument to function is the fully matched text. Other captured groups are subsequent arguments. This strategy can be applied recursively.
1 2 3 4 5 6 7 | |
Split method
split method can take both string or a regular expression.
An example of split using a string.
1 2 3 | |
An example of split using regular expression.
1 2 3 4 | |
Non capturing Group
The requirement given to me states that I should strictly look for word java, ruby or rails within word boundary. This can be done like this.
1 2 3 | |
Above code works. However notice the code duplication. This can be refactored to the one given below.
1 2 3 | |
Above code works and there is no code duplication. However in this case I am asking regular expression engine to create a captured group which I’ll not be using. Regex engines need to do extra work to keep track of captured groups. It would be nice if I could say to regex engine do not capture this into a group because I will not be using it.
?: is a special symbol that tells regex engine to create non capturing group. Above code can be refactored into the one given below.
1 2 3 | |
1 2 3 | |