Regex Methods
JavaScript offers several regex methods on strings: match(), matchAll(), search(), replace(), replaceAll(), and split(). The RegExp prototype has test() and exec().
JavaScript offers several regex methods on strings: match(), matchAll(), search(), replace(), replaceAll(), and split(). The RegExp prototype has test() and exec().
const str = "Hello World, Hello JS";
str.match(/hello/gi); // ["Hello", "Hello"]
str.search(/world/i); // 6 (index)
str.replace(/hello/gi, "Hi"); // "Hi World, Hi JS"
str.split(/,\s*/); // ["Hello World", "Hello JS"]
/\d+/.test("abc123"); // true
/\d+/.exec("abc123"); // ["123", index: 3, ...]
Use matchAll() (returns an iterator) instead of exec() in a loop — it is cleaner for finding all matches with capture groups.
More in JavaScript