100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

JavaScript Regex Cheat Sheet

JavaScript Regex Cheat Sheet

Covers regular expression syntax, flags, common validation patterns, named capture groups, and the string and RegExp methods used to apply them.

1 PageIntermediateApr 15, 2026

Regex Syntax Basics

Core building blocks of a pattern.

javascript
/abc/          // Literal match/a|b/          // Alternation: a OR b/ab?/          // ? = 0 or 1 of preceding (matches "a" or "ab")/ab*/          // * = 0 or more/ab+/          // + = 1 or more/a{2,4}/       // Between 2 and 4 of preceding/^abc$/        // ^ start, $ end of string (or line, with /m flag)/[abc]/        // Character class: a, b, or c/[^abc]/       // Negated class: anything but a, b, c/./            // Any character except newline (unless /s flag)/\d \w \s/     // Digit, word char, whitespace ( \D \W \S = negated)/(abc)/        // Capturing group/(?:abc)/      // Non-capturing group/(?<year>\d{4})/  // Named capturing group

Testing & Matching

RegExp and String methods for applying patterns.

javascript
const re = /\d+/;re.test("order 42");         // true -- does it match at all?"order 42".match(re);        // ["42", index: 6, ...] -- first match detailsconst global = /\d+/g;"a1 b22 c333".match(global);        // ["1", "22", "333"] -- all matches, no groups[..."a1 b22 c333".matchAll(/\d+/g)]; // Array of full match objects (needs /g flag)"2024-01-15".replace(/(\d+)-(\d+)-(\d+)/, "$2/$3/$1");// "01/15/2024" -- $1, $2, $3 reference captured groups"a,b, c ,d".split(/\s*,\s*/);   // ["a", "b", "c", "d"] -- split on pattern

Common Patterns

Pragmatic patterns for everyday validation.

javascript
const email = /^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$/;const url = /^https?:\/\/[\w.-]+\.[a-zA-Z]{2,}(\/\S*)?$/;const digitsOnly = /^\d+$/;const whitespaceCollapse = /\s+/g;         // "a   b" -> "a b" via replace(re, " ")const usPhone = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;"  hello  world  ".replace(whitespaceCollapse, " ").trim();// "hello world"// Note: these are pragmatic, not RFC-perfect (e.g. real email validation is far more complex)

Named Groups & Lookaround

Extract structured data and match with context.

javascript
const match = "2024-01-15".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);match.groups.year;    // "2024"match.groups.month;   // "01"// Lookahead: match "foo" only if followed by "bar"/foo(?=bar)/.test("foobar");    // true/foo(?=bar)/.test("foobaz");    // false// Negative lookahead: match "foo" NOT followed by "bar"/foo(?!bar)/.test("foobaz");    // true// Lookbehind: match digits preceded by "$""$100".match(/(?<=\$)\d+/)[0];  // "100"

Flags & Methods Reference

Flags change how the whole pattern behaves.

  • g- Global: find all matches, not just the first
  • i- Case-insensitive matching
  • m- Multiline: ^ and $ match the start/end of each line
  • s- Dotall: . also matches newline characters
  • u- Unicode: enables full Unicode code point matching
  • y- Sticky: matches only from lastIndex, no scanning ahead
  • str.search(re)- Returns the index of the first match, or -1
  • re.exec(str)- Returns the next match; with /g, advances lastIndex on repeated calls
Pro Tip

A /g regex object is stateful -- it remembers lastIndex between calls to .exec() or .test(), which causes intermittent bugs if you reuse the same regex instance across unrelated strings; create a fresh literal or reset lastIndex = 0 first.

Was this cheat sheet helpful?

Explore Topics

#JavaScriptRegex#JavaScriptRegexCheatSheet#Programming#Intermediate#RegexSyntaxBasics#TestingMatching#CommonPatterns#NamedGroupsLookaround#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet