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/gonhat hot h\nt→ hathot\dA digit 0 to 9.
/\d/gona1b22→ 122\DAnything that is not a digit.
/\D+/gon12ab3c→ abc\wA word character: A–Z, a–z, 0–9 or underscore (no accented letters).
/\w+/gonsnake_case café→ snake_casecaf\WAnything that is not a word character.
/\W/gona-b c→ -\sWhitespace: space, tab, line breaks and other Unicode spaces.
/\s+/gona b\tc→ \t\SAnything that is not whitespace.
/\S+/gonone two→ onetwo[abc]Any one of the listed characters.
/[aeiou]/gonregex→ ee[^abc]Any one character not listed.
/[^0-9 ]+/gontel 123 ext→ telext[a-z]A range of characters.
/[A-F0-9]+/goncode 3FA9 or BEEF→ 3FA9BEEF\t \n \rTab, line feed (new line) and carriage return.
/\r?\n/gona\r\nb\nc→ \r\n\n
Anchors and boundaries
^Start of the text (or of each line with the m flag).
/^\w+/gmonone two\nthree→ onethree$End of the text (or of each line with the m flag).
/\w+$/gmonone two\nthree→ twothree\bA word boundary: between a word character and a non-word character.
/\bcat\b/goncat concat cat.→ catcat\BNot a word boundary.
/\Bcat/goncat concat→ cat
Quantifiers
*Zero or more of the previous item (as many as possible).
/bo*/gonb bo booo→ bbobooo+One or more.
/bo+/gonb bo booo→ bobooo?Zero or one (optional).
/colou?r/goncolor colour→ colorcolour{n}Exactly n times.
/\d{4}/gon12 2026 123456→ 20261234{n,}n or more times.
/\d{3,}/gon12 123 12345→ 12312345{n,m}Between n and m times.
/\d{2,3}/gon1 12 1234→ 12123*? +? ?? {n,m}?Lazy versions: match as few as possible.
/<.+?>/gon<b>bold</b>→ <b></b>
Groups and references
(…)Capturing group: groups items and remembers the match as group 1, 2, …
/(\d+)-(\d+)/gon10-20 30-40→ 10-2030-40(?:…)Non-capturing group: groups items without remembering them.
/(?:ab)+/gonababx ab→ ababab(?<name>…)Named capturing group, read as match.groups.name.
/(?<year>\d{4})-(?<month>\d{2})/gonfrom 2026-11 to 2027-01→ 2026-112027-01a|bAlternation: either side.
/cat|dog/goncat bird dog→ catdog\1Back-reference to what group 1 matched.
/\b(\w+) \1\b/gonthe the cat sat sat→ the thesat sat\k<name>Back-reference to a named group.
/(?<q>["']).*?\k<q>/gonsay "hi" or 'yo'→ "hi"'yo'
Lookahead and lookbehind
(?=…)Lookahead: followed by …, without including it in the match.
/\d+(?=%)/gon50% of 200→ 50(?!…)Negative lookahead: not followed by ….
/\b\d+\b(?!%)/gon50% of 200→ 200(?<=…)Lookbehind: preceded by ….
/(?<=₹)\d+/gon₹250 and $40→ 250(?<!…)Negative lookbehind: not preceded by ….
/(?<![₹\d])\d+/gon₹250 and 40→ 40
Flags
gGlobal: find all matches, not just the first.
/a/gonbanana→ aaaiIgnore case.
/jag/gionJag JAG jag→ JagJAGjagmMultiline: ^ and $ match at each line start and end.
/^-.*$/gmon- one\ntext\n- two→ - one- twosdotAll: . also matches line breaks.
/a.b/sona\nb→ a\nbuUnicode: treat the pattern as code points and allow \p{…}.
/^.$/uon😀→ 😀vunicodeSets: 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]]+/gvonabcÉtéXY→ ÉéXYySticky: match only at lastIndex, no searching ahead.
/\d/yon1a2→ 1dhasIndices: also report start and end positions of each group.
/b(c)/donabcd→ 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}+/guonnaïve नमस्ते 42→ naïveनमसत\p{Script=Devanagari}Characters of one script, here Devanagari (Hindi, Marathi…).
/\p{Script=Devanagari}+/guonHello नमस्ते→ नमस्ते\p{N}Any number character, including other scripts’ digits.
/\p{N}+/guonRoom ४२ or 42→ ४२42\P{…}Not in the property (the opposite of \p).
/\P{L}+/guonab12cd→ 12\u{hhhh}A character by its code point (needs u or v).
/\u{20B9}\d+/guonPay ₹99→ ₹99
Escaping special characters
\. \* \? \+ \( \) \[ \] \{ \} \| \^ \$ \\ \/Put a backslash before a special character to match it literally.
/\$\d+\.\d{2}/goncost $4.50 or $12.00→ $4.50$12.00\xhhA character by its two-digit hex code.
/\x41/gonABA→ AA
Everyday patterns (starting points, not full validation)
Simple email shapeSomething@something.something. Real address rules are far wider; to be sure, send a confirmation email.
/[^\s@]+@[^\s@]+\.[^\s@]+/gonmail a.b@example.co.in or x@y→ a.b@example.co.inIndian PIN codeSix digits, the first not 0 (shape only; it does not check that the PIN exists).
/\b[1-9]\d{5}\b/gonJaipur 302001, code 012345→ 302001Date YYYY-MM-DDISO-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/gon2026-11-19 and 2026-13-01→ 2026-11-19Hex colour#RGB or #RRGGBB.
/#(?:[0-9a-fA-F]{3}){1,2}\b/goncolor: #fff; bg: #1D4ED8; bad: #12→ #fff#1D4ED8Trim spacesSpaces at the start or end of each line (replace with nothing).
/^[ \t]+|[ \t]+$/gmona \n b→Repeated wordThe same word twice in a row, ignoring case.
/\b(\w+)\s+\1\b/gionThis 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
- ECMAScript Language Specification: RegExp (Regular Expression) Objects (last checked 2026-11-19)
- MDN Web Docs: Regular expressions syntax cheat sheet (last checked 2026-11-19)
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
\wcovers 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
- Search for what you need, such as “lookbehind” or “digit”, or jump to a section.
- Read the meaning and the example, with its matches highlighted.
- 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.