JavaScript: the Web Programming Language

JavaScript Regular Expressions

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(), replace(), replaceAll(), search(), and split() methods of String.


To learn more, click to read a thorough discussion of JavaScript Regular Expressions

Creating regular expressions

A regular expression is typically created as a literal by enclosing a pattern in forward slashes (/):

const regex1 = /ab+c/g;

Regular expressions can also be created with the RegExp() constructor:

const regex2 = new RegExp("ab+c", "g");

They have no runtime differences, although they may have implications on performance, static analyzability, and authoring ergonomic issues with escaping characters.