Methods of RegExp and String

Methods of RegExp and String

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:

  1. In case there is no g flag in the regexp, it returns the initial match as an array with the properties indexinput, 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:

  1. It doesn’t return an array, but an iterable object with matches. A regular array can be made from it with Array.from.
  2. Each match is returned as an array with capturing groups.
  3. 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 matchAllArray.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):

  1. match –it’s the match,
  2. p1, p2, ..., pn – the capturing group contents (if there exist any),
  3. offset – the match position,
  4. input – the string of the source,,
  5. 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:

It happens as in the second test regexp.lastIndex is non-zero. For working around that, it is possible to set regexp.lastIndex = 0 before every search.Rather than calling methods on regexp, you can use string methods str.match/search/… that don’t use lastIndex.
Reactions

Post a Comment

0 Comments

close