Regular Expressions: Advanced Techniques
Published by Brayan K
A regular expression (regex) is a pattern used to search, match, and replace text, letting you describe complex string rules — like email or phone formats — with a compact, special syntax.
Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Decide whether you are searching, validating or consuming a grammar
A regular expression that finds a useful substring does not prove that the entire input is valid. A tokenizer that searches forward can also hide an unexpected character. Start with the contract, then choose boundaries, flags and an API that enforces it. Strings & String Methods owns ordinary string searching and slicing; this lesson owns regex state, captures, assertions and controlled tokenization.
const search = /[A-Z]{2}-[0-9]{3}/;
const whole = /^[A-Z]{2}-[0-9]{3}$/;
for (const value of ["AB-123", "prefix AB-123 suffix", "AB-123\n"]) {
console.log(search.test(value), whole.test(value)); // No multiline flag: require this full format.
}
const match = whole.exec("AB-123");
console.log(match[0], match.index); // The complete matched text and its start offset.
// ✅ Expected output:
// true true
// true false
// true false
// AB-123 0This is an exercise identifier format, not an email, phone or general Unicode-name validator. With the multiline flag, ^ and $ can mark line boundaries instead of only the input boundaries. Choose that behaviour deliberately.
A regex literal and a JavaScript string have different escape layers
const literal = /\d+/;
const correct = new RegExp("\\d+");
const missingSlash = new RegExp("\d+");
console.log(literal.exec("item 12")[0], correct.exec("item 12")[0]);
console.log(missingSlash.source, missingSlash.test("item 12"));
// ✅ Expected output:
// 12 12
// d+ falseThe constructor needs a string containing a backslash followed by d. In an ordinary string literal, that backslash must itself be escaped. Regex syntax errors are errors in the resulting pattern, so inspect pattern.source when debugging dynamically constructed expressions.
const text = "fileXtxt file.txt";
console.log(new RegExp("file.txt").exec(text)[0]);
console.log(text.includes("file.txt"), text.indexOf("file.txt"));
console.log(/file\.txt/.exec(text)[0]);
// ✅ Expected output:
// fileXtxt
// true 9
// file.txtFor a literal search, includes or indexOf can avoid regex syntax entirely. Modern runtimes provide RegExp.escape for safely embedding literal text into a pattern; check your supported runtimes or use a standards-compliant polyfill. The Node 22 runtime used for this course’s checks does not provide that method, so these portable exercises do not silently call it or present a partial homemade replacement as universal.
Global and sticky expressions carry a cursor
const pattern = /a/g;
console.log(pattern.test("a"), pattern.lastIndex);
console.log(pattern.test("a"), pattern.lastIndex);
console.log(pattern.test("a"), pattern.lastIndex);
console.log(/a/.test("a"), /a/.test("a"));
// ✅ Expected output:
// true 1
// false 0
// true 1
// true trueWith g or y, successful exec/test updates lastIndex and a failed match resets it. Reusing a global regex as a stateless validation predicate can therefore alternate results. Use a non-global expression for a yes/no whole-input check, or explicitly own the cursor when scanning.
const search = /\d+/g;
const sticky = /\d+/y;
console.log(search.exec("x12").index);
console.log(sticky.exec("x12") === null, sticky.lastIndex);
sticky.lastIndex = 1;
const match = sticky.exec("x12");
console.log(match[0], match.index, sticky.lastIndex);
// ✅ Expected output:
// 1
// true 0
// 12 1 3The global search finds digits later in the string; sticky matching must start at lastIndex. Save the current position before a failure because the engine resets lastIndex. The final tokenizer uses this refusal to skip as part of its input-validation contract.
Captures carry data; non-capturing groups and assertions carry structure
const pattern = /^(?<name>[A-Za-z]+):(?<quantity>[0-9]+)(?:!(?<flag>[A-Z]+))?$/;
for (const line of ["pens:12!LOW", "books:0"]) {
const match = pattern.exec(line);
console.log(match.groups.name, match.groups.quantity, match.groups.flag === undefined);
}
console.log(pattern.test("pens:12 trailing"));
// ✅ Expected output:
// pens 12 false
// books 0 true
// falseCaptured quantities are still strings; a format match is not numeric range validation. The optional flag can be absent, and the non-capturing group does not add another numbered capture. Named groups improve intent, but the grammar still needs domain limits.
const text = "cost=12 fee=3";
console.log(text.match(/(?<=cost=)\d+/)[0]);
console.log("ALPHA: BETA".match(/[A-Z]+(?=:)/)[0]);
console.log(/^(?!admin$)[a-z]+$/.test("admin"), /^(?!admin$)[a-z]+$/.test("reader"));
// ✅ Expected output:
// 12
// ALPHA
// false trueLookbehind checks the preceding cost= context; lookahead checks the following colon. Neither becomes part of the matched digits or word. These examples use fixed, supported syntax in the checked runtimes. Assertions do not replace a parser for nested structures or turn an incomplete identifier rule into a security policy.
const repeated = /^(?<word>[A-Za-z]+) \k<word>$/i;
console.log(repeated.test("Go go"), repeated.test("Go stop"));
const replaced = "name=Rae".replace(/^(?<key>[a-z]+)=(?<value>[A-Za-z]+)$/, "$<value> ($<key>)");
console.log(replaced);
// ✅ Expected output:
// true false
// Rae (name)Greedy and lazy choose different spans, not different parsing languages
const text = '"first" "second"';
console.log(text.match(/".+"/g).join("|"));
console.log(text.match(/".+?"/g).join("|"));
console.log(text.match(/"[^"]*"/g).join("|"));
// ✅ Expected output:
// "first" "second"
// "first"|"second"
// "first"|"second"The negated character class directly excludes quotes for this deliberately simple format. None of these patterns handles escaped quotes, nested syntax or general HTML. Lazy quantifiers can still backtrack. Use JSON.parse for JSON, a suitable CSV parser for CSV and DOM parsing APIs for HTML instead of expanding this toy delimiter pattern into a universal parser.
Unicode matching does not automatically mean user-perceived characters
const ascii = /^\w+$/;
const letters = /^\p{L}[\p{L}\p{M}]*$/u;
for (const text of ["Cafe", "Café", "Δelta", "e\u0301"]) {
console.log(ascii.test(text), letters.test(text));
}
console.log(/^.$/.test("😀"), /^.$/u.test("😀"));
console.log(/^.$/u.test("e\u0301"));
// ✅ Expected output:
// true true
// false true
// false true
// false true
// false true
// falseThe Unicode-aware dot treats the emoji as one code point, while the decomposed accented letter is two code points. A grapheme can contain several code points. The property pattern includes combining marks after the first letter, but it is still a chosen name format rather than every human naming convention.
const composed = "é", decomposed = "e\u0301";
console.log(composed === decomposed, composed === decomposed.normalize("NFC"));
const match = /A/u.exec("😀A");
console.log(match.index, "😀".length, [..."😀"].length);
// ✅ Expected output:
// false true
// 2 2 1JavaScript regex indices and string slicing use UTF-16 code-unit offsets, even with u. Normalizing text may change those offsets relative to the original input, so retain an index mapping if you must highlight the original. Do not label a regex index as a grapheme count.
Choose an iteration API that advances after empty matches
const empty = /(?:)/g;
for (let count = 0; count < 2; count++) {
const match = empty.exec("A");
console.log(match.index, empty.lastIndex);
}
console.log([...("😀A".matchAll(/(?:)/gu))].map(match => match.index).join(","));
// ✅ Expected output:
// 0 0
// 0 0
// 0,2,3The manual loop is deliberately bounded so it cannot freeze the editor. A naive while(exec(...)) loop over an empty-match pattern can fail to advance. matchAll handles empty-match progress and yields capture records. A regex passed to String.matchAll must have g; its own cursor is used through a cloned matcher rather than being repeatedly mutated by the iterator.
console.log([...("A1 B2".matchAll(/\d+/))].length);
// ✅ Expected error:
// TypeError: String.prototype.matchAll called with a non-global RegExp argumentconst nested = /^(a+)+$/;
const simple = /^a+$/;
for (const value of ["aaaa", "aaaa!"]) {
console.log(nested.test(value), simple.test(value));
}
// ✅ Expected output:
// true true
// false falseThe small examples agree on acceptance. The nested quantifier admits many ways to divide a failing sequence; increasing adversarial input can make backtracking costly. This is not a benchmark. Prefer an unambiguous pattern, bound input according to the real contract and test rejected cases. Anchoring or adding a lazy suffix alone does not prove a pattern safe.
Guided exercise: extract a named quantity without accepting trailing junk
Fill the literal hints. Capture a name and decimal digits, require the complete input, and convert the captured quantity only after the format matches.
const pattern = /^(?<name>[A-Za-z]+):(?<quantity>[0-9]+)$/;
const match = pattern.___("pens:12"); // 👉 replace ___ with exec
console.log(match.groups.name, Number(match.groups.___)); // 👉 replace ___ with quantity
console.log(pattern.___("pens:12x")); // 👉 replace ___ with test
// ✅ Expected output:
// pens 12
// falseIndependent brief: tokenize a small expression without skipping errors
Write tokenize(source) for strings of at most 4096 UTF-16 code units. Recognize ASCII names beginning with a letter or underscore, decimal digit runs, the single-character punctuation = + ( ), and spaces/tabs. Skip only the specified whitespace. Return type, original value, start and end offsets for each non-whitespace token. Keep integers as text to avoid accidental precision loss.
Use sticky matching from an explicit position; reject the first unmatched character with its original offset. Require every successful token to consume input. Empty input is valid. This is lexical analysis, not an expression parser: adjacent tokens such as a number followed by a name are returned separately, and syntax/meaning belongs to a later parser. Check whitespace, names, large digit runs, unexpected minus/newline/emoji, repeated independent calls and non-string/overlong input.
function tokenize(source) {
if (typeof source !== "string") throw new TypeError("source must be a string");
if (source.length > 4096) throw new RangeError("source exceeds the exercise limit");
const token = /(?<space>[ \t]+)|(?<integer>[0-9]+)|(?<name>[A-Za-z_][A-Za-z0-9_]*)|(?<punct>[=+()])/y;
const result = [];
let position = 0;
while (position < source.length) {
token.lastIndex = position;
const match = token.exec(source);
if (!match) throw new SyntaxError("Unexpected character at " + position);
const end = token.lastIndex;
if (end <= position) throw new Error("token pattern must consume input");
if (match.groups.space === undefined) {
const type = match.groups.integer !== undefined ? "integer" :
match.groups.name !== undefined ? "name" : "punct";
result.push({ type, value: match[0], start: position, end });
}
position = end;
}
return result;
}
console.log(JSON.stringify(tokenize("sum = (12 + x_2)")));
console.log(JSON.stringify(tokenize(" \t")));
console.log(JSON.stringify(tokenize("9007199254740993")));
console.log(tokenize("a").length, tokenize("a").length);
for (const input of ["a-b", "a\nb", "a😀b"]) {
try { tokenize(input); } catch (error) { console.log(error.name, error.message); }
}
for (const input of [null, "a".repeat(4097)]) {
try { tokenize(input); } catch (error) { console.log(error.name, error.message); }
}
// ✅ Expected output:
// [{"type":"name","value":"sum","start":0,"end":3},{"type":"punct","value":"=","start":4,"end":5},{"type":"punct","value":"(","start":6,"end":7},{"type":"integer","value":"12","start":7,"end":9},{"type":"punct","value":"+","start":10,"end":11},{"type":"name","value":"x_2","start":12,"end":15},{"type":"punct","value":")","start":15,"end":16}]
// []
// [{"type":"integer","value":"9007199254740993","start":0,"end":16}]
// 1 1
// SyntaxError Unexpected character at 1
// SyntaxError Unexpected character at 1
// SyntaxError Unexpected character at 1
// TypeError source must be a string
// RangeError source exceeds the exercise limitReference notes
MDN documents lastIndex, matchAll, Unicode properties and RegExp.escape. The runnable corpus uses features checked in both Node runtimes and the browser; no catastrophic long-input timing test is embedded in the lesson.
Practice quiz
What does this complete program print? const pattern=/a/g;console.log(pattern.test("a"),pattern.test("a"),pattern.test("a"));
- true false true
- true true true
- false true false
- true false false
Answer: true false true. The cursor advances on success and resets after failure.
What does this complete program print? const pattern=/[0-9]+/y;console.log(pattern.exec("x12")===null);pattern.lastIndex=1;console.log(pattern.exec("x12")[0]);
- false 12
- true 12
- true 1
- false 1
Answer: true 12. Sticky matching starts exactly at the specified cursor.
What does this complete program print? const match=/^(?<name>[A-Z]+):(?<count>[0-9]+)$/.exec("A:12");console.log(typeof match.groups.count,match.groups.name);
- number A
- string 12
- string A
- number 12
Answer: string A. Captures are text until explicitly converted.
What does this complete program print? console.log(/^.$/u.test("😀"),/^.$/u.test("e\u0301"));
- false false
- true true
- false true
- true false
Answer: true false. Unicode dot consumes one code point, not an arbitrary grapheme.
What does this complete program print? console.log(/A/u.exec("😀A").index);
- 2
- 0
- 1
- 3
Answer: 2. Indices remain UTF-16 code-unit offsets.
What does this complete program print? console.log([...("A".matchAll(/(?:)/g))].map(match=>match.index).join(","));
- 0
- 0,1
- 1
- 0,0
Answer: 0,1. matchAll advances after the empty match and includes the end position.
What does this complete program print? console.log(/^(?<word>[a-z]+) \k<word>$/.test("go go"));
- false
- undefined
- true
- SyntaxError
Answer: true. The named backreference matches the same captured word.
What does this complete program print? console.log(/^[A-Z]+$/.test("ABC "),/^[A-Z]+$/m.test("ABC "));
- true true
- false false
- true false
- false true
Answer: false true. The multiline flag permits a line boundary before the newline.
Continue this course
- Previous: Error Handling Patterns in Large JS Apps
- Next: Working with Dates, Timezones & Intl API — Parse, format, and localise dates correctly across timezones
- Quick reference: JavaScript cheat sheet · Regex cheat sheet › Lookarounds