Methods accepting RegExp

There are seven methods provided by JavaScript that are capable of utilizing regular expressions:

  • RegExp.prototype.test(String): Runs the regular expression against the passed string and return true if it finds at least one match. It will return false if no matches are found.
  • RegExp.prototype.exec(String): If the regular expression has a global (g), flag then exec() will return the next match from the current lastIndex (and will update the regular expression's lastIndex after doing so); otherwise, it will return the first match of the regular expression (similar to String.prototype.match).
  • String.prototype.match(RegExp): This String method will return a match (or if the global flag is set, all matches) of the passed regular expression made against the string.
  • String.prototype.replace(RegExp, Function): This String method will execute the passed function on every single match and will, for each match, replace the matched text with whatever the function returns.
  • String.prototype.matchAll(RegExp): This String method will return an iterator of all results and their individual groups. This is useful when you have a global regular expression with individual matching groups.
  • String.prototype.search(RegExp): This String method will return the index of the first match or -1 if there are no matches found.
  • String.prototype.split(RegExp): This String method will return an array containing parts of the string split by the provided separator (which can be a regular expression).

There are many methods to choose from, but for most situations, you'll likely find that the RegExp method, test(), and the String methods, match() and replace(), are the most useful.

Here's a rundown of some examples of these methods. This should give you an idea of the situations in which each method may be used:

// RegExp.prototype.test
/@/.test('[email protected]'); // => true
/@/.test('aaa.com'); // => false

// RegExp.prototype.exec
const regexp = /d+/g;
const string = '123 456 789';
regex.exec(string); // => ["123"]
regex.exec(string); // => ["456"]
regex.exec(string); // => ["789"]
regex.exec(string); // => null

// String.prototype.match
'Orders: #92838 #02812 #92833'.match(/d+/); // => ["92838"]
'Orders: #92838 #02812 #92833'.match(/wo+w/g); // => ["92838", "02812", "92833"]

// String.prototype.matchAll
const string = 'Orders: #92333 <fulfilled> #92835 <pending>';
const matches = [
...string.matchAll(/#(d+) <(w+)>/g)
];
matches[0][1]; // => 92333
matches[0][2]; // => fulfilled

// String.prototype.replace
'1 2 3 4'.replace(/d/, n => `<${n}>`); // => "<1> 2 3 4'
'1 2 3 4'.replace(/d/g, n => `<${n}>`); // => "<1> <2> <3> <4>'

// String.prototype.search
'abcdefghhijklmnop'.search(/k/); // => 11

// String.prototype.split
'time_in____a__tree'.split(/_+/); // ["time", "in", "a", "tree"]

Most of these methods, as you can see, behave intuitively. However, there is some complexity surrounding stickiness and the lastIndex property, which we will now go over.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset