{
    "slug": "js_regex",
    "term": "Regular Expressions in JavaScript",
    "category": "javascript",
    "difficulty": "intermediate",
    "short": "JS regex syntax, flags (g, i, m, s, u, d), methods (test, match, matchAll, replace, split), and the critical differences from PHP's PCRE.",
    "long": "JS regex: /pattern/flags or new RegExp(pattern, flags). Key methods: test() (returns boolean), match() (returns array or null), matchAll() (returns iterator of all matches — requires g flag), replace()/replaceAll() (string replacement), split() (split by pattern). Flags: g (global — all matches), i (case-insensitive), m (multiline — ^ and $ match line boundaries), s (dotAll — . matches newlines), u (Unicode — enables \\u{1F600} and correct Unicode handling), d (indices — adds match index info). Named capture groups: (?<name>pattern). Lookahead: (?=) positive, (?!) negative.",
    "aliases": [
        "JavaScript regex",
        "RegExp",
        "regular expression JS"
    ],
    "tags": [
        "javascript",
        "regex",
        "strings"
    ],
    "misconception": "JavaScript regex is the same as PHP PCRE — JS regex lacks some PCRE features (no lookbehind in older engines, no \\K, different flag syntax), and the g flag with test() has a stateful lastIndex that causes surprising behaviour.",
    "why_it_matters": "The stateful g flag with RegExp.test() in a loop is a famous JS gotcha — test() advances lastIndex on each call, causing alternating true/false for a pattern that should always match.",
    "common_mistakes": [
        "Using RegExp.test() with g flag in a loop — lastIndex advances causing false negatives.",
        "Not using the u flag for Unicode strings — \\w does not match accented characters without u.",
        "match() without g flag returns only first match — use matchAll() for all matches.",
        "Not anchoring patterns — /cat/ matches concatenate, not just the word cat."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "regex_pcre_php",
        "regex_performance",
        "js_tagged_templates"
    ],
    "prerequisites": [
        "regex_pcre_php",
        "js_closures",
        "string_algorithms"
    ],
    "refs": [
        "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions"
    ],
    "bad_code": "// Stateful g flag bug — alternating results:\nconst re = /hello/g; // g flag makes lastIndex stateful\nconsole.log(re.test('hello world')); // true  (lastIndex=5)\nconsole.log(re.test('hello world')); // false (lastIndex=0 after miss)\nconsole.log(re.test('hello world')); // true  (lastIndex=5 again)\n\n// match() only returns first:\n'aaa'.match(/a/);  // ['a'] — not all three!",
    "good_code": "// Avoid g flag with test() — or reset lastIndex:\nconst hasHello = /hello/.test('hello world'); // No g — safe\n\n// matchAll for all matches:\nconst matches = [...'aaa'.matchAll(/a/g)]; // [{...}, {...}, {...}]\n\n// Named capture groups:\nconst dateRe = /(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})/u;\nconst { groups: { year, month, day } } = '2026-03-16'.match(dateRe);",
    "quick_fix": "Add the /u flag to all regexes matching user text to enable Unicode mode; use named capture groups (?<year>\\d{4}) for readable matches; test with online tools before production",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/js_regex",
        "html_url": "https://codeclaritylab.com/glossary/js_regex",
        "json_url": "https://codeclaritylab.com/glossary/js_regex.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Regular Expressions in JavaScript](https://codeclaritylab.com/glossary/js_regex) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/js_regex"
            }
        }
    }
}