Methods of RegExp and String
On this page, you will see how various methods work in-depth with regular expressions in JavaScript.
Particularly, we will cover methods such as str.match(regexp), str.matchAll(regexp), str.split(regexp|substr, limit), str.search(regexp), str.replace(str|regexp, str|func), regexp.exec(str), and regexp.test(str).
str.match(regexp)¶
The str.match(regexp) method detects matches for regexp in the string str.
This method includes the following three modes:
- In case there is no g flag in the regexp, it returns the initial match as an array with the properties index, input, as well as capturing groups, like here:
In case you want to get the result as an array, you should write it as follows:
let result = str.match(regexp) || [];
str.matchAll(regexp)¶
The str.matchAll(regexp) method is an enhanced version of the str.match method. Primarily, it is used to look for all matches with all groups. But, it has three differences from str.match, that are as follows:
- It doesn’t return an array, but an iterable object with matches. A regular array can be made from it with Array.from.
- Each match is returned as an array with capturing groups.
- In case of having no matches, it returns an empty iterable object, not null.
An example of using the str.matchAll(regexp) looks like this:
Using for..of for looping over the matches of matchAll, Array.from is not needed.
str.split(regexp|substr, limit)¶
This method helps to split the string with regexp as a delimiter.
The split method can be used with strings, in this way:
There is an essential limitation, you should note: only the first match is found by the search.
For example, if you want to find more matches, you can use another method such as str.matchAll(regexp).
str.replace(str|regexp, str|func)¶
This is a generic method and is used to search and replace one of the most efficient ones.
It can be used without regexps for searching and replacing a substring, like this:
For situations, requiring smart replacement, the second argument can act as a function.
That function can be called with arguments func(match, p1, p2, ..., pn, offset, input, groups):
- match –it’s the match,
- p1, p2, ..., pn – the capturing group contents (if there exist any),
- offset – the match position,
- input – the string of the source,,
- groups – named groups object.
In case no parentheses exist in the regexp, only three arguments should be used func(str, offset, input).
Here is an example of uppercasing all the matches:
When you use a function, it gives you ultimate replacement power: it receives all the information about the match, having access to outer variables.
regexp.exec(str)¶
The regexp.exec(str) method is used for returning a match for the regexp in the string str. The main difference of this method is that it is called on a regexp, not a string.
Its behavior is different depending on whether the regexp includes the g flag or not.
In case of having no g flag, it returns the first match, like the str.match(regexp) method. It won’t bring anything new.
In case of having the str.match(regexp) flag, the following happens:
- Calling regexp.exec(str) will return the first match, saving the position right after it in the regexp.lastIndex property.
- The following such call begins the search from the regexp.lastIndex position, returning the next match and saving the position after that in regexp.lastIndex.
- In case of finding no matches, regexp.exec will return null and reset regexp.lastIndex to 0.
The regexp.exec method can be used to look from a specific position by manually setting lastIndex.
Here is an example:
When you use a function, it gives you ultimate replacement power: it receives all the information about the match, having access to outer variables.
regexp.exec(str)¶
The regexp.exec(str) method is used for returning a match for the regexp in the string str. The main difference of this method is that it is called on a regexp, not a string.
Its behavior is different depending on whether the regexp includes the g flag or not.
In case of having no g flag, it returns the first match, like the str.match(regexp) method. It won’t bring anything new.
In case of having the str.match(regexp) flag, the following happens:
- Calling regexp.exec(str) will return the first match, saving the position right after it in the regexp.lastIndex property.
- The following such call begins the search from the regexp.lastIndex position, returning the next match and saving the position after that in regexp.lastIndex.
- In case of finding no matches, regexp.exec will return null and reset regexp.lastIndex to 0.
The regexp.exec method can be used to look from a specific position by manually setting lastIndex.
Here is an example:
0 Comments
CAN FEEDBACK
Emoji