/error/iFast and readable when the pattern is static.
JavaScript RegExp syntax, APIs, flags, named captures, and high-value browser/Node recipes.
Core JavaScript regex syntax and methods.
/error/iFast and readable when the pattern is static.
new RegExp("^" + prefix + "\\d+$", "i")Useful when the pattern is assembled at runtime.
/error/i.test(message)Returns `true` or `false`.
/(\d{4})-(\d{2})-(\d{2})/.exec(dateStr)Returns an array-like match object or `null`.
text.match(/\b\w+\b/g)With `g`, returns all matches; without `g`, returns the first detailed match.
Array.from(text.matchAll(/(?<key>\w+)=(?<val>\w+)/g))Useful when you need every match and capture groups.
text.replace(/\s+/g, " ")Common for cleanup and normalization.
text.replace(/(\w+),\s*(\w+)/g, (_, last, first) => `${first} ${last}`)Useful for reformatting structured text.
csvLine.split(/\s*,\s*/)Removes optional whitespace around commas.
Flags, named captures, and replacement patterns in JavaScript.
/pattern/gimsuydJavaScript supports several flags; combine only the ones you need.
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/Read captures using `match.groups.year` in modern runtimes.
/(?<q>["\']).*?\k<q>/Useful for matching paired quotes.
text.replace(/(\w+)\s+(\w+)/, "$2, $1")Convenient for simple reordering.
text.replace(/(?<h>\d{2}):(?<m>\d{2})/, (...args) => { const groups = args.at(-1); return `${groups.h}h ${groups.m}m`; })Named groups improve readability in larger replacements.
/^\p{L}+$/uRequires the `u` flag.
High-value browser and Node regex recipes.
/^[^\s@]+@[^\s@]+\.[^\s@]+$/A lightweight client-side check, not a full RFC parser.
/^[a-z0-9]+(?:-[a-z0-9]+)*$/Good for URLs, IDs, and content slugs.
/([^&=?#]+)=([^&=#]*)/gUse `matchAll` to iterate all pairs.
text.trim().replace(/\s+/g, " ")Very common cleanup recipe.
text.replace(/\bBearer\s+[A-Za-z0-9._-]+\b/g, "Bearer [REDACTED]")Useful when sanitizing output.
/#(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})\b/gCovers 3, 6, and 8 digit forms.
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/Handy for tooling and validation.