❤ Like
🔖 Save
🔗 Share
@thedevspaceio
@thedevspaceio

Regular Expression Cheatsheet

@thedevspaceio

🔍 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

js
const re = /hello/;
re.test("hello world"); // true

RegExp constructor

js
const re = new RegExp("hello", "i"); // same as /hello/i
re.test("HELLO world"); // true

Flags

FlagDescription
gGlobal search.
iCase-insensitive.
mMultiline mode.
sDotall mode (. matches newlines).
uUnicode mode.
ySticky mode.

g: Global

Finds all matches instead of stopping at the first.

js
"hello world".match(/o/g); // ["o", "o"]
"a1b2c3".replace(/\d/g, "x"); // "axbxcx"

i: Case-insensitive

Matches letters regardless of case.

js
/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.

js
const text = "hello\nworld";
/^world$/m.test(text); // true

s: Dotall

Makes . match newline characters too.

js
const html = "<div>\n  hello\n</div>";
/<div>.*<\/div>/s.test(html); // true

u: Unicode

Enables full Unicode matching and lets you use \u{} escapes and Unicode property escapes.

js
/\u{1F600}/u.test("😀"); // true
/\p{Emoji}/u.test("😀"); // true

y: Sticky

Matches only from the lastIndex position and does not search further.

js
const re = /\d+/y;
re.lastIndex = 2;
"ab12cd".match(re); // ["12"]
 
re.lastIndex = 0;
"ab12cd".match(re); // null

Character classes

PatternMatches
.Any character except newline (Any character with s flag).
[a-z]Any character in the range.
\dAny digit (0-9).
\DAny non-digit.
\wAny word character (a-z, A-Z, 0-9, _).
\WAny non-word character.
\sAny whitespace character.
\SAny non-whitespace character.
[abc]Any character in the set.
[^abc]Any character not in the set.

.: Any character

js
"a".match(/./); // ["a"]
"abc".match(/./g); // ["a", "b", "c"]

[a-z]: Character range

js
"Hello".match(/[a-z]/); // ["e"]
"HELLO".match(/[a-z]/); // null

\d: Digit

js
"abc123".match(/\d+/); // ["123"]
"Room 42".match(/\d+/); // ["42"]

\D: Non-digit

js
"abc123".match(/\D+/); // ["abc"]
"2024!".match(/\D/); // ["!"]

\w: Word character

js
"hello_world".match(/\w+/); // ["hello_world"]
"user_123".match(/\w+/); // ["user_123"]

\W: Non-word character

js
"hello world".match(/\W/); // [" "]
"a@b".match(/\W/); // ["@"]

\s: Whitespace

js
"a b".match(/\s/); // [" "]
"a\tb".match(/\s/); // ["\t"]

\S: Non-whitespace

js
" a".match(/\S/); // ["a"]
"  hi".match(/\S+/); // ["hi"]

[abc]: Character set

js
"cat".match(/[aeiou]/); // ["a"]
"sky".match(/[aeiou]/); // null

[^abc]: Negated character set

js
"cat".match(/[^aeiou]/); // ["c"]
"aei".match(/[^aeiou]/); // null

Quantifiers

PatternMeaning
*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

js
"aaab".match(/a*/); // ["aaa"]
"b".match(/a*/); // [""]

+: One or more

js
"aaab".match(/a+/); // ["aaa"]
"b".match(/a+/); // null

?: Zero or one

js
"color".match(/colou?r/); // ["color"]
"colour".match(/colou?r/); // ["colour"]

{n}: Exactly n times

js
"123".match(/\d{3}/); // ["123"]
"12".match(/\d{3}/); // null

{n,}: At least n times

js
"12345".match(/\d{3,}/); // ["12345"]
"12".match(/\d{3,}/); // null

{n,m}: Between n and m times

js
"1234".match(/\d{2,4}/); // ["1234"]
"1".match(/\d{2,4}/); // null

Anchors

PatternMatches
^Start of string (or line in m mode).
$End of string (or line in m mode).
\bWord boundary.
\BNon-word boundary.

^: Start of string

js
/^hello/.test("hello world"); // true
/^world/.test("hello world"); // false

$: End of string

js
/world$/.test("hello world"); // true
/hello$/.test("hello world"); // false

\b: Word boundary

js
/\bworld\b/.test("hello world"); // true
/\bworld\b/.test("helloworld"); // false

\B: Non-word boundary

js
/\Bworld\B/.test("helloworld"); // true
/\Bworld\B/.test("hello world"); // false

Groups

PatternDescription
(...)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

js
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

js
const match = "foo".match(/(?:foo|bar)/); // ["foo"]
match[0]; // "foo"

Named capturing group

js
const match = "2024-07-12".match(
  /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/,
);
match.groups.year; // "2024"

Lookarounds

PatternDescription
(?=...)Positive lookahead.
(?!...)Negative lookahead.
(?<=...)Positive lookbehind.
(?<!...)Negative lookbehind.

(?=...): Positive lookahead

Matches a group only if followed by another pattern.

js
/\d+(?=px)/.exec("100px"); // ["100"]
/\d+(?=px)/.exec("100em"); // null

(?!...): Negative lookahead

Matches a group only if not followed by another pattern.

js
/\d+(?!px)/.exec("100em"); // ["100"]
/\d+(?!px)/.exec("100px"); // null

(?<=...): Positive lookbehind

Matches a group only if preceded by another pattern.

js
/(?<=\$)\d+/.exec("Price: $42"); // ["42"]
/(?<=\$)\d+/.exec("Price: 42"); // null

(?<!...): Negative lookbehind

Matches a group only if not preceded by another pattern.

js
/(?<!\$)\d+/.exec("Price: 42"); // ["42"]
/(?<!\$)\d+/.exec("Price: $42"); // null

JavaScript regex methods

.test()

Returns true if the pattern matches.

js
/hello/.test("hello world"); // true

.match()

Returns an array of matches.

js
"abc123".match(/\d+/); // ["123"]
"a1b2c3".match(/\d/g); // ["1", "2", "3"]

.matchAll()

Returns an iterator of all matches with capture groups.

js
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.

js
"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.

js
"a b c".replaceAll(" ", "-"); // "a-b-c"

.search()

Returns the index of the first match, or -1.

js
"hello world".search(/world/); // 6

.split()

Splits a string using a regex.

js
"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.

@thedevspaceio
www.thedevspace.io