The next intake step needs to recognize and clean up email-like text. Run script.js and inspect its Console output; regular expressions work on strings, without any page markup.
A regular expression (regex) is a pattern that describes a set of strings. JavaScript has built-in regex support for searching, validating, and transforming strings.
// Literal syntax for a fixed pattern
const pattern = /hello/i; // i = case-insensitive
// The i flag ignores letter case; without it, HELLO does not match /hello/.
| Pattern | Meaning |
|---|---|
. | Any character except newline (in basic regex mode) |
\d | Digit (0-9) |
\w | Word character (letter, digit, _) |
\s | Whitespace |
^ | Start of string |
$ | End of string |
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{n,m} | Between n and m |
[abc] | Character class |
[^abc] | Negated class |
// test() — returns boolean
/^\d{5}$/.test("12345"); // true — zip code
/^\d{5}$/.test("1234"); // false
// match() — returns array of matches
"I have 3 cats and 12 dogs".match(/\d+/g); // ["3", "12"]
// replace() — string substitution
"hello world".replace(/\b\w/g, c => c.toUpperCase()); // "Hello World"
// split() with regex
"one, two, three".split(/,\s*/); // ["one", "two", "three"]
isValidEmail(email) with the basic shape regex /^[^\s@]+@[^\s@]+\.[^\s@]+$/ and return its boolean result. This checks shape, not deliverability.extractNumbers(text) to return all digit-sequence strings, or an empty array when none occur.maskEmail(email) for a valid address with a nonempty local part: retain its first character, replace the rest with *, and keep the domain.valid: true | numbers: 3,12 | masked: a****@example.com.Test a pattern: pattern.test(str) returns true/false.
Find matches: str.match(pattern) returns an array or null.
Use flags: g for global, i for case-insensitive.