SyntaxStudy
Sign Up
JavaScript Intermediate 5 min read

String and RegExp Methods

Regex Methods

JavaScript offers several regex methods on strings: match(), matchAll(), search(), replace(), replaceAll(), and split(). The RegExp prototype has test() and exec().

Example
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, ...]
Pro Tip

Use matchAll() (returns an iterator) instead of exec() in a loop — it is cleaner for finding all matches with capture groups.