Regex cheatsheet
A searchable reference for JavaScript and PHP patterns.
Characters
.Any characterAny character except a line break. With the s flag it matches line breaks too.a.c matches abc, a-c\dDigitA digit 0 to 9.\d{4} matches 2025\DNot a digitAny character that is not a digit.\wWord characterA letter, digit or underscore.\w+ matches user_1\WNot a word characterAnything except a letter, digit or underscore.\sWhitespaceSpace, tab, line break and similar.\SNot whitespaceAny non-whitespace character.\hHorizontal whitespacePHP onlyA space or tab.\RAny line breakPHP onlyMatches \n, \r\n or \r as one unit.\n \r \tLine feed, carriage return, tabControl characters.\xhhHex characterThe character with hex code hh.\x41 matches A\uhhhhUnicode characterJavaScript onlyA character by its 4-digit code point.\u00e9 matches é\x{hhhh}Unicode characterPHP onlyA character by code point. Needs the u flag.\x{1F600}\p{L}Unicode letterAny letter in any language. Needs the u flag.\p{L}+ matches Ünïcode\p{Lu}Uppercase letterUnicode uppercase letters. Needs the u flag.\p{N}Unicode numberAny numeric character. Needs the u flag.\p{Emoji}EmojiJavaScript onlyEmoji characters. Needs the u flag.
Character sets
[abc]Any ofOne character from the set.[aeiou] matches vowels[^abc]None ofOne character not in the set.[a-z]RangeAny character in the range.[0-9a-f] matches hex digits[[:alpha:]]POSIX classPHP onlyLetters. Also :digit:, :alnum:, :space:, :upper:, :lower:, :punct:.
Anchors
^StartStart of the text. With the m flag, start of each line.$EndEnd of the text. With the m flag, end of each line.\bWord boundaryBetween a word character and a non-word character.\bcat\b matches cat, not concat\BNot a word boundaryAnywhere that is not a word boundary.\AStart of subjectPHP onlyVery start of the text, ignoring the m flag.\zEnd of subjectPHP onlyVery end of the text.\ZEnd or before final newlinePHP onlyEnd of the text, allowing one trailing line break.
Quantifiers
*Zero or moreGreedy: as many as possible.a* matches '', a, aaa+One or moreGreedy: at least one.\d+ matches 7, 2025?OptionalZero or one.colou?r matches color, colour{n}Exactly nExactly n times.\d{3}{n,}n or moreAt least n times.{n,m}Between n and mFrom n to m times.\d{2,4}*? +? ??LazyAdd ? to match as few as possible.<.+?> matches one tag at a time*+ ++ ?+PossessivePHP onlyMatch greedily and never give back. Prevents backtracking.
Groups
(abc)Capture groupGroups text and remembers the match as $1, $2 and so on.(\d+)-(\d+)(?:abc)Non-capturing groupGroups without remembering the match.(?<name>abc)Named groupCapture with a name. Reuse with \k<name>.(?<year>\d{4})(?P<name>abc)Named group (PCRE)PHP onlyOlder PCRE syntax. (?<name>) also works in PHP 5.2.2+.\1 \k<name>BackreferenceMatches the same text as an earlier group.(\w)\1 matches aa, bba|bAlternationMatches a or b.cat|dog(?>abc)Atomic groupPHP onlyMatches once and never backtracks into the group.(?i)Inline flagPHP onlyTurn on a flag from this point, such as (?i) for case-insensitive.(?R)RecursionPHP onlyMatches the whole pattern again, for nested structures.
Lookaround
(?=abc)Positive lookaheadFollowed by abc, without consuming it.\d+(?=px) matches 12 in 12px(?!abc)Negative lookaheadNot followed by abc.(?<=abc)Positive lookbehindPreceded by abc.(?<=\$)\d+ matches 5 in $5(?<!abc)Negative lookbehindNot preceded by abc.\KReset match startPHP onlyDrops everything matched so far from the result.foo\Kbar
Flags
iIgnore caseLetters match in either case.gGlobalJavaScript onlyFind all matches, not just the first. In PHP use preg_match_all.mMultiline^ and $ match at each line.sDot matches newlineLets . match line breaks.uUnicodeTreat the pattern and text as Unicode. Needed for \p{...}.yStickyJavaScript onlyMatch only at lastIndex.dIndicesJavaScript onlyReport start and end positions of each group.vUnicode setsJavaScript onlyStricter Unicode mode with set operations in classes.xExtendedPHP onlyIgnore whitespace and allow # comments in the pattern.UUngreedyPHP onlySwaps greedy and lazy quantifiers.DDollar end onlyPHP only$ matches only at the very end.
Replacement
$1 $2Group referenceInsert a captured group.$<name>Named group referenceJavaScript onlyInsert a named group.${name}Named group referencePHP onlyInsert a named group.$&Whole matchJavaScript onlyInsert the entire match.$0 \0Whole matchPHP onlyInsert the entire match.$$Literal dollarJavaScript onlyInsert a $ sign.
Common patterns
^\S+@\S+\.\S+$Simple emailA basic shape check. Fully validating email needs more than a regex.^\d{4}-\d{2}-\d{2}$ISO dateMatches 2025-03-01. Does not check the date is real.^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$Hex colorMatches #fff and #1f5bd8.^https?://[^\s/$.?#].[^\s]*$URLA simple http or https link.^\s+|\s+$Trim whitespaceMatches leading and trailing whitespace. Replace with nothing.\s{2,}Repeated whitespaceTwo or more whitespace characters in a row.
Runs in your browser. Nothing you enter is sent anywhere.