Regular Expression Cheatsheet
🔍 JavaScript Regex Cheatsheet
Regular expressions are patterns for matching text. They are often used for validation, search-and-replace, parsing, and extracting structured data from strings.
✅ Regex features ✅ Creating regex ✅ Flags ✅ Character classes ✅ Quantifiers ✅ Anchors ✅ Groups ✅ Lookarounds ✅ Common methods
#javascript #regex #regular expressions #patternmatching #validation #lookahead #capturegroups #stringmethods #flags #webdev
Creating regex
Regular expressions are patterns used to match character combinations in strings.
Literal syntax
const re = /hello/;
re.test("hello world"); // trueRegExp constructor
const re = new RegExp("hello", "i"); // same as /hello/i
re.test("HELLO world"); // trueFlags
| Flag | Description |
|---|---|
g | Global search. |
i | Case-insensitive. |
m | Multiline mode. |
s | Dotall mode (. matches newlines). |
u | Unicode mode. |
y | Sticky mode. |
g: Global
Finds all matches instead of stopping at the first.
"hello world".match(/o/g); // ["o", "o"]
"a1b2c3".replace(/\d/g, "x"); // "axbxcx"i: Case-insensitive
Matches letters regardless of case.
/hello/i.test("HELLO"); // true
"Hello World".match(/hello/i); // ["Hello"]m: Multiline
Changes ^ and $ to match the start and end of each line instead of the whole string.
const text = "hello\nworld";
/^world$/m.test(text); // trues: Dotall
Makes . match newline characters too.
const html = "<div>\n hello\n</div>";
/<div>.*<\/div>/s.test(html); // trueu: Unicode
Enables full Unicode matching and lets you use \u{} escapes and Unicode property escapes.
/\u{1F600}/u.test("😀"); // true
/\p{Emoji}/u.test("😀"); // truey: Sticky
Matches only from the lastIndex position and does not search further.
const re = /\d+/y;
re.lastIndex = 2;
"ab12cd".match(re); // ["12"]
re.lastIndex = 0;
"ab12cd".match(re); // nullCharacter classes
| Pattern | Matches |
|---|---|
. | Any character except newline (Any character with s flag). |
[a-z] | Any character in the range. |
\d | Any digit (0-9). |
\D | Any non-digit. |
\w | Any word character (a-z, A-Z, 0-9, _). |
\W | Any non-word character. |
\s | Any whitespace character. |
\S | Any non-whitespace character. |
[abc] | Any character in the set. |
[^abc] | Any character not in the set. |
.: Any character
"a".match(/./); // ["a"]
"abc".match(/./g); // ["a", "b", "c"][a-z]: Character range
"Hello".match(/[a-z]/); // ["e"]
"HELLO".match(/[a-z]/); // null\d: Digit
"abc123".match(/\d+/); // ["123"]
"Room 42".match(/\d+/); // ["42"]\D: Non-digit
"abc123".match(/\D+/); // ["abc"]
"2024!".match(/\D/); // ["!"]\w: Word character
"hello_world".match(/\w+/); // ["hello_world"]
"user_123".match(/\w+/); // ["user_123"]\W: Non-word character
"hello world".match(/\W/); // [" "]
"a@b".match(/\W/); // ["@"]\s: Whitespace
"a b".match(/\s/); // [" "]
"a\tb".match(/\s/); // ["\t"]\S: Non-whitespace
" a".match(/\S/); // ["a"]
" hi".match(/\S+/); // ["hi"][abc]: Character set
"cat".match(/[aeiou]/); // ["a"]
"sky".match(/[aeiou]/); // null[^abc]: Negated character set
"cat".match(/[^aeiou]/); // ["c"]
"aei".match(/[^aeiou]/); // nullQuantifiers
| Pattern | Meaning |
|---|---|
* | Zero or more. |
+ | One or more. |
? | Zero or one. |
{n} | Exactly n times. |
{n,} | At least n times. |
{n,m} | Between n and m times. |
*: Zero or more
"aaab".match(/a*/); // ["aaa"]
"b".match(/a*/); // [""]+: One or more
"aaab".match(/a+/); // ["aaa"]
"b".match(/a+/); // null?: Zero or one
"color".match(/colou?r/); // ["color"]
"colour".match(/colou?r/); // ["colour"]{n}: Exactly n times
"123".match(/\d{3}/); // ["123"]
"12".match(/\d{3}/); // null{n,}: At least n times
"12345".match(/\d{3,}/); // ["12345"]
"12".match(/\d{3,}/); // null{n,m}: Between n and m times
"1234".match(/\d{2,4}/); // ["1234"]
"1".match(/\d{2,4}/); // nullAnchors
| Pattern | Matches |
|---|---|
^ | Start of string (or line in m mode). |
$ | End of string (or line in m mode). |
\b | Word boundary. |
\B | Non-word boundary. |
^: Start of string
/^hello/.test("hello world"); // true
/^world/.test("hello world"); // false$: End of string
/world$/.test("hello world"); // true
/hello$/.test("hello world"); // false\b: Word boundary
/\bworld\b/.test("hello world"); // true
/\bworld\b/.test("helloworld"); // false\B: Non-word boundary
/\Bworld\B/.test("helloworld"); // true
/\Bworld\B/.test("hello world"); // falseGroups
| Pattern | Description |
|---|---|
(...) | Capturing group. Stores the matched text for later use. |
(?:...) | Non-capturing group. Groups without storing. |
(?<name>...) | Named capturing group. Stores the match under a name. |
Capturing group
const match = "2024-07-12".match(/(\d{4})-(\d{2})-(\d{2})/);
match[0]; // "2024-07-12"
match[1]; // "2024"
match[2]; // "07"
match[3]; // "12"Non-capturing group
const match = "foo".match(/(?:foo|bar)/); // ["foo"]
match[0]; // "foo"Named capturing group
const match = "2024-07-12".match(
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/,
);
match.groups.year; // "2024"Lookarounds
| Pattern | Description |
|---|---|
(?=...) | Positive lookahead. |
(?!...) | Negative lookahead. |
(?<=...) | Positive lookbehind. |
(?<!...) | Negative lookbehind. |
(?=...): Positive lookahead
Matches a group only if followed by another pattern.
/\d+(?=px)/.exec("100px"); // ["100"]
/\d+(?=px)/.exec("100em"); // null(?!...): Negative lookahead
Matches a group only if not followed by another pattern.
/\d+(?!px)/.exec("100em"); // ["100"]
/\d+(?!px)/.exec("100px"); // null(?<=...): Positive lookbehind
Matches a group only if preceded by another pattern.
/(?<=\$)\d+/.exec("Price: $42"); // ["42"]
/(?<=\$)\d+/.exec("Price: 42"); // null(?<!...): Negative lookbehind
Matches a group only if not preceded by another pattern.
/(?<!\$)\d+/.exec("Price: 42"); // ["42"]
/(?<!\$)\d+/.exec("Price: $42"); // nullJavaScript regex methods
.test()
Returns true if the pattern matches.
/hello/.test("hello world"); // true.match()
Returns an array of matches.
"abc123".match(/\d+/); // ["123"]
"a1b2c3".match(/\d/g); // ["1", "2", "3"].matchAll()
Returns an iterator of all matches with capture groups.
const matches = "a1b2".matchAll(/[a-z](\d)/g);
for (const match of matches) {
console.log(match[0], match[1]);
}.replace()
Replaces the first match, or all matches with the g flag.
"hello world".replace(/world/, "JS"); // "hello JS"
"a b c".replace(/\s/g, "-"); // "a-b-c".replaceAll()
Replaces all matches without needing the g flag.
"a b c".replaceAll(" ", "-"); // "a-b-c".search()
Returns the index of the first match, or -1.
"hello world".search(/world/); // 6.split()
Splits a string using a regex.
"a,b;c".split(/[,;]/); // ["a", "b", "c"]Full-Stack AI Developer Roadmap
From HTML & CSS to working with AI models, all in one structured roadmap.