String Match in JavaScript

Level 257 keystrokes

With the g flag, match() returns an array of all matches of a regular expression in a string.

JavaScript code

// Create a string with repeated pattern
1const str = "cat bat rat";
// match() with the g flag returns every "at" as an array: ["at", "at", "at"]
2console.log(str.match(/at/g));

Output

[ 'at', 'at', 'at' ]
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • ;× 2
  • .× 2
  • "× 2
  • (× 2
  • )× 2
  • /× 2
  • =× 1

More JavaScript examples at level 2

← JavaScript typing practice