Skip to content
DoItJag

Regex Cheat Sheet

A searchable quick reference for JavaScript regular expressions. Every entry has a short example whose matches were checked by running it, and a Try it box where you can change the pattern and text and see the matches instantly.

53 entries

Character classes

  • .

    Any single character except line breaks (add the s flag to include them).

    /h.t/g on hat hot h\nt → hathot

  • \d

    A digit 0 to 9.

    /\d/g on a1b22 → 122

  • \D

    Anything that is not a digit.

    /\D+/g on 12ab3c → abc

  • \w

    A word character: A–Z, a–z, 0–9 or underscore (no accented letters).

    /\w+/g on snake_case café → snake_casecaf

  • \W

    Anything that is not a word character.

    /\W/g on a-b c → -

  • \s

    Whitespace: space, tab, line breaks and other Unicode spaces.

    /\s+/g on a b\tc → \t

  • \S

    Anything that is not whitespace.

    /\S+/g on one two → onetwo

  • [abc]

    Any one of the listed characters.

    /[aeiou]/g on regex → ee

  • [^abc]

    Any one character not listed.

    /[^0-9 ]+/g on tel 123 ext → telext

  • [a-z]

    A range of characters.

    /[A-F0-9]+/g on code 3FA9 or BEEF → 3FA9BEEF

  • \t \n \r

    Tab, line feed (new line) and carriage return.

    /\r?\n/g on a\r\nb\nc → \r\n\n

Anchors and boundaries

  • ^

    Start of the text (or of each line with the m flag).

    /^\w+/gm on one two\nthree → onethree

  • $

    End of the text (or of each line with the m flag).

    /\w+$/gm on one two\nthree → twothree

  • \b

    A word boundary: between a word character and a non-word character.

    /\bcat\b/g on cat concat cat. → catcat

  • \B

    Not a word boundary.

    /\Bcat/g on cat concat → cat

Quantifiers

  • *

    Zero or more of the previous item (as many as possible).

    /bo*/g on b bo booo → bbobooo

  • +

    One or more.

    /bo+/g on b bo booo → bobooo

  • ?

    Zero or one (optional).

    /colou?r/g on color colour → colorcolour

  • {n}

    Exactly n times.

    /\d{4}/g on 12 2026 123456 → 20261234

  • {n,}

    n or more times.

    /\d{3,}/g on 12 123 12345 → 12312345

  • {n,m}

    Between n and m times.

    /\d{2,3}/g on 1 12 1234 → 12123

  • *? +? ?? {n,m}?

    Lazy versions: match as few as possible.

    /<.+?>/g on <b>bold</b> → <b></b>

Groups and references

  • (…)

    Capturing group: groups items and remembers the match as group 1, 2, …

    /(\d+)-(\d+)/g on 10-20 30-40 → 10-2030-40

  • (?:…)

    Non-capturing group: groups items without remembering them.

    /(?:ab)+/g on ababx ab → ababab

  • (?<name>…)

    Named capturing group, read as match.groups.name.

    /(?<year>\d{4})-(?<month>\d{2})/g on from 2026-11 to 2027-01 → 2026-112027-01

  • a|b

    Alternation: either side.

    /cat|dog/g on cat bird dog → catdog

  • \1

    Back-reference to what group 1 matched.

    /\b(\w+) \1\b/g on the the cat sat sat → the thesat sat

  • \k<name>

    Back-reference to a named group.

    /(?<q>["']).*?\k<q>/g on say "hi" or 'yo' → "hi"'yo'

Lookahead and lookbehind

  • (?=…)

    Lookahead: followed by …, without including it in the match.

    /\d+(?=%)/g on 50% of 200 → 50

  • (?!…)

    Negative lookahead: not followed by ….

    /\b\d+\b(?!%)/g on 50% of 200 → 200

  • (?<=…)

    Lookbehind: preceded by ….

    /(?<=₹)\d+/g on ₹250 and $40 → 250

  • (?<!…)

    Negative lookbehind: not preceded by ….

    /(?<![₹\d])\d+/g on ₹250 and 40 → 40

Flags

  • g

    Global: find all matches, not just the first.

    /a/g on banana → aaa

  • i

    Ignore case.

    /jag/gi on Jag JAG jag → JagJAGjag

  • m

    Multiline: ^ and $ match at each line start and end.

    /^-.*$/gm on - one\ntext\n- two → - one- two

  • s

    dotAll: . also matches line breaks.

    /a.b/s on a\nb → a\nb

  • u

    Unicode: treat the pattern as code points and allow \p{…}.

    /^.$/u on 😀 → 😀

  • v

    unicodeSets: like u, plus set operations in classes such as [\p{L}--[a-z]].

    Newer feature (ES2024); check browser support for the browsers you target.

    /[\p{L}--[a-z]]+/gv on abcÉtéXY → ÉéXY

  • y

    Sticky: match only at lastIndex, no searching ahead.

    /\d/y on 1a2 → 1

  • d

    hasIndices: also report start and end positions of each group.

    /b(c)/d on abcd → bc

Unicode

  • \p{L}

    Any letter in any script (needs the u or v flag).

    Hindi and other Indic vowel signs are marks (\p{M}), not letters, so words break apart. Use [\p{L}\p{M}]+ for whole words.

    /\p{L}+/gu on naïve नमस्ते 42 → naïveनमसत

  • \p{Script=Devanagari}

    Characters of one script, here Devanagari (Hindi, Marathi…).

    /\p{Script=Devanagari}+/gu on Hello नमस्ते → नमस्ते

  • \p{N}

    Any number character, including other scripts’ digits.

    /\p{N}+/gu on Room ४२ or 42 → ४२42

  • \P{…}

    Not in the property (the opposite of \p).

    /\P{L}+/gu on ab12cd → 12

  • \u{hhhh}

    A character by its code point (needs u or v).

    /\u{20B9}\d+/gu on Pay ₹99 → ₹99

Escaping special characters

  • \. \* \? \+ \( \) \[ \] \{ \} \| \^ \$ \\ \/

    Put a backslash before a special character to match it literally.

    /\$\d+\.\d{2}/g on cost $4.50 or $12.00 → $4.50$12.00

  • \xhh

    A character by its two-digit hex code.

    /\x41/g on ABA → AA

Everyday patterns (starting points, not full validation)

  • Simple email shape

    Something@something.something. Real address rules are far wider; to be sure, send a confirmation email.

    /[^\s@]+@[^\s@]+\.[^\s@]+/g on mail a.b@example.co.in or x@y → a.b@example.co.in

  • Indian PIN code

    Six digits, the first not 0 (shape only; it does not check that the PIN exists).

    /\b[1-9]\d{5}\b/g on Jaipur 302001, code 012345 → 302001

  • Date YYYY-MM-DD

    ISO-style date shape; it does not reject 2026-02-31.

    /\b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\b/g on 2026-11-19 and 2026-13-01 → 2026-11-19

  • Hex colour

    #RGB or #RRGGBB.

    /#(?:[0-9a-fA-F]{3}){1,2}\b/g on color: #fff; bg: #1D4ED8; bad: #12 → #fff#1D4ED8

  • Trim spaces

    Spaces at the start or end of each line (replace with nothing).

    /^[ \t]+|[ \t]+$/gm on a \n b →

  • Repeated word

    The same word twice in a row, ignoring case.

    /\b(\w+)\s+\1\b/gi on This is is a The the test → is isThe the

Try it

Runs with your browser’s JavaScript engine. For capture groups and longer texts, use the Regex Tester.

Sources

A cheat sheet you can trust

Regular expressions are compact, which makes them easy to misremember. Each entry on this sheet has a short example, and every example has been run in JavaScript and checked against the matches printed here.

The sheet covers JavaScript, the flavour used in browsers, Node.js and most web forms. Python, PHP, Java and .NET share the basics (classes, quantifiers, groups) but differ in details such as lookbehind length, Unicode support and flag names.

How to read an entry

The left column shows the syntax. Next to it is the meaning in plain words, then an example written as /pattern/flags, the sample text and the highlighted matches. Press Try it to copy the example into the tester at the bottom, where you can change anything and see new matches as you type.

Worked example

Look up lookbehind. The example /(?<=₹)\d+/g on the text “₹250 and $40” matches only 250: the rupee sign must come right before the digits, but it is not part of the match. Change the pattern in the tester to (?<=\$)\d+ and the match becomes 40.

Common traps

  • \w covers only English letters, digits and underscore. For Hindi, Tamil or accented words, use \p{L} with the u flag, and add \p{M} so vowel signs stay inside the word.
  • Greedy quantifiers grab as much as they can: .+ between two tags swallows everything up to the last one. Add ? to make them lazy.
  • Nested quantifiers such as (a+)+ can take a very long time on text that almost matches. The tester stops after 1.5 seconds instead of freezing.

Patterns are starting points

The everyday patterns at the end (email shape, PIN code, date, colour) check form only. They accept dates like 2026-02-31 and miss unusual but valid email addresses. For complete testing with capture groups and long texts, use the Regex Tester; to apply a pattern to a document, try Find and Replace.

How to use Regex Cheat Sheet

  1. Search for what you need, such as “lookbehind” or “digit”, or jump to a section.
  2. Read the meaning and the example, with its matches highlighted.
  3. Press Try it to load the example into the tester and change it for your own text.

Privacy: how it works

Everything happens in your browser. Nothing you type is sent to any server.

Frequently asked questions

Does this work for Python, PHP or Java?

Most of the basics are the same, but this sheet describes JavaScript. Flags, lookbehind rules, Unicode properties and named groups differ between languages, so test in the language you use.

Are the email and date patterns enough for validation?

No. They check the shape only. A date pattern accepts 31 February, and real email rules are much wider; confirm with a real check, such as sending an email.

What if my pattern freezes?

The Try it box runs in a background thread and stops after 1.5 seconds, so a runaway pattern shows a warning instead of hanging the page.