Regular Expression (JavaScript)

A regular expression (regex) is a pattern used to search, match, and replace text. In JavaScript you write them between slashes, like /pattern/.

JavaScript

Regex describes a set of strings using special characters — for example \d matches a digit and + means "one or more". Methods like test(), match(), and replace() use them.

Regex is powerful for validation (emails, phone numbers) and text processing, but can be tricky to read.

Syntax

/pattern/flags

Examples

JavaScript

const hasDigit = /\d/;
console.log(hasDigit.test("abc5"));
console.log("a1b2c3".replace(/\d/g, "#"));

Output:

true
a#b#c#

See also

Back to the Dictionary