JavaScript regular expression basics
This tool tests JavaScript regular expressions using the browser's native ECMAScript RegExp engine.
In JavaScript, regular expressions can be written as a literal or constructed using the RegExp object. Use a regex literal when the pattern is static in source code, and use RegExp when the pattern or flags are constructed dynamically at runtime.
// Regex literal (static pattern)
const regex = /\b[A-Za-z]+\b/g;
// RegExp constructor (dynamic pattern)
const regex = new RegExp("\\b[A-Za-z]+\\b", "g");- Character Classes: \d (digits), \w (word characters), \s (whitespace), . (wildcard except newlines)
- Quantifiers: * (zero or more), + (one or more), ? (optional / zero or one), {n,m} (between n and m)
- Anchors: ^ (start of input / line with m flag) and $ (end of input / line with m flag)