{"success":true,"name":"javascript-replace-changes-only-the-first-match-choose-repla","description":"Working in JavaScript and a string replacement leaves later occurrences untouched","category":"ai-brimind","path":"ai-brimind/javascript-replace-changes-only-the-first-match-choose-repla/SKILL.md","file":"SKILL.md","content":"---\nname: javascript-replace-changes-only-the-first-match-choose-repla\ndescription: \"Working in JavaScript and a string replacement leaves later occurrences untouched\"\n---\n\n# JavaScript replace() Changes Only the First Match: Choose replaceAll on Purpose\n\n## When to use\nProject is JavaScript, and code uses string replace with a plain string pattern where every occurrence must change, or replacement output contains odd $ artifacts.\n\n## Example request\ntemplate with three {name} placeholders → only the first is filled; single-match replace. sanitizing all spaces in a filename → one space fixed, the rest remain; partial cleanup. replacement text containing $ → it's interpreted as a special pattern and mangles output; unescaped replacement token. — each assumed replace means replace-everywhere.\n\n## Instructions\nAct as a proofreader who knows the difference between 'fix this typo once' and 'fix it throughout the manuscript'. In JavaScript, replace with a plain string pattern substitutes only the first occurrence — a silent partial job that passes short tests and fails on real text. When every occurrence must change, say so in code: use replaceAll, or a regex with the global flag; when truly only the first should change, keep replace and it reads as intended. Two traps around the corner: dollar signs in the replacement string are special (they reference match groups), so replacement text that may contain $ — prices, user input — must be escaped or inserted via the function form of replace, which treats nothing specially; and when building a regex from user-provided text, escape the regex special characters first, or dots and brackets in their input will silently change what matches. Test replacements on a string containing the target three times and a $ in the replacement. Only proceed once each replacement states its scope — first or all — as a decision, not a default nobody chose.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #754\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}