JavaScript Curriculum
Describing Patterns in Text
mediumNexus receives user-submitted data: usernames, emails, bio text. The validation code is a maze of indexOf() calls, split() chains, and charAt() checks — 80 lines to validate an email. Regular expressions compress that into a single pattern that's simultaneously more powerful and more readable, once you know the syntax.
What is a regular expression?
A regular expression (regex) is a pattern that describes a set of strings. Instead of writing if (str.includes('@') && str.includes('.')), you write a pattern that matches exactly the strings you want.
The /pattern/flags syntax is a regex literal — similar to how "string" is a string literal. Alternatively: new RegExp('pattern', 'flags').
The syntax building blocks
| Pattern | Meaning | Example match |
|---|---|---|
. | Any character except newline | a.c matches abc, axc |
\d | Digit (0–9) | \d\d matches 42 |
\w | Word character (a–z, A–Z, 0–9, _) | \w+ matches hello_42 |
\s | Whitespace (space, tab, newline) | \s+ matches |
[abc] | Character class — any of a, b, c | [aeiou] matches vowels |
[^abc] | Not any of a, b, c | [^\d] matches non-digits |
[a-z] | Range — any lowercase letter | [a-zA-Z] any letter |
\b | Word boundary | \bcat\b matches "cat" not "cats" |
^ | Start of string | ^Hello |
$ | End of string | world$ |
Quantifiers
| Pattern | Meaning |
|---|---|
* | Zero or more |
+ | One or more |
? | Zero or one (optional) |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{2,} | 2 or more |
Flags
Live testing — try the patterns
The four string/regex methods
regex.test(string) → booleanreturns: true or falsecode
const emailRe = /[\w.]+@[\w]+\.[a-z]{2,}/i
emailRe.test('hello@nexus.app') // true
emailRe.test('not an email') // false
emailRe.test('ADMIN@NEXUS.IO') // true (i flag)output
true false true
Use .test() when you only need yes/no — faster than .match() because it stops at the first match.
Capture groups
Parentheses create a capture group — a sub-match you can extract:
Groups in .replace():
Common patterns worth knowing
Your challenge
@\w+ is the core pattern: @ is a literal @, \w+ matches one or more word characters (letters, digits, underscores). With the g flag, .match() returns all matches as an array. Guard against null: return text.match(/@\w+/g) ?? [] covers the no-match case neatly using the nullish coalescing operator from lesson-13.
Challenge
Write a function called extractMentions(text) that finds all @username mentions in a string. A mention is @ followed by one or more word characters (letters, digits, underscores). Use a regex with the global flag. Return the array of matches (the full @username strings), or an empty array if there are none. Test with: 'Hello @alex and @jordan_99, cc @sam!' — should return ['@alex', '@jordan_99', '@sam'].
Describing Patterns in Text
mediumNexus receives user-submitted data: usernames, emails, bio text. The validation code is a maze of indexOf() calls, split() chains, and charAt() checks — 80 lines to validate an email. Regular expressions compress that into a single pattern that's simultaneously more powerful and more readable, once you know the syntax.
What is a regular expression?
A regular expression (regex) is a pattern that describes a set of strings. Instead of writing if (str.includes('@') && str.includes('.')), you write a pattern that matches exactly the strings you want.
The /pattern/flags syntax is a regex literal — similar to how "string" is a string literal. Alternatively: new RegExp('pattern', 'flags').
The syntax building blocks
| Pattern | Meaning | Example match |
|---|---|---|
. | Any character except newline | a.c matches abc, axc |
\d | Digit (0–9) | \d\d matches 42 |
\w | Word character (a–z, A–Z, 0–9, _) | \w+ matches hello_42 |
\s | Whitespace (space, tab, newline) | \s+ matches |
[abc] | Character class — any of a, b, c | [aeiou] matches vowels |
[^abc] | Not any of a, b, c | [^\d] matches non-digits |
[a-z] | Range — any lowercase letter | [a-zA-Z] any letter |
\b | Word boundary | \bcat\b matches "cat" not "cats" |
^ | Start of string | ^Hello |
$ | End of string | world$ |
Quantifiers
| Pattern | Meaning |
|---|---|
* | Zero or more |
+ | One or more |
? | Zero or one (optional) |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{2,} | 2 or more |
Flags
Live testing — try the patterns
The four string/regex methods
regex.test(string) → booleanreturns: true or falsecode
const emailRe = /[\w.]+@[\w]+\.[a-z]{2,}/i
emailRe.test('hello@nexus.app') // true
emailRe.test('not an email') // false
emailRe.test('ADMIN@NEXUS.IO') // true (i flag)output
true false true
Use .test() when you only need yes/no — faster than .match() because it stops at the first match.
Capture groups
Parentheses create a capture group — a sub-match you can extract:
Groups in .replace():
Common patterns worth knowing
Your challenge
@\w+ is the core pattern: @ is a literal @, \w+ matches one or more word characters (letters, digits, underscores). With the g flag, .match() returns all matches as an array. Guard against null: return text.match(/@\w+/g) ?? [] covers the no-match case neatly using the nullish coalescing operator from lesson-13.
Challenge
Write a function called extractMentions(text) that finds all @username mentions in a string. A mention is @ followed by one or more word characters (letters, digits, underscores). Use a regex with the global flag. Return the array of matches (the full @username strings), or an empty array if there are none. Test with: 'Hello @alex and @jordan_99, cc @sam!' — should return ['@alex', '@jordan_99', '@sam'].