JavaScript
Replace every literal substring
Use replaceAll with a string search value when every literal occurrence should be replaced. It avoids constructing a regular expression for input that is meant to be plain text.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const text = "a.b a.b";
console.log(text.replaceAll("a.b", "item"));
console.log(text);How it works
The dot is treated as a literal dot because the search value is a string. The returned string contains the replacements, and the original text remains unchanged. This is suitable for controlled text substitutions such as changing a separator or a known token.
Limits and mistakes
Replacement strings interpret dollar-sign patterns such as $&. If replacement text must be completely literal, supply a callback returning that text. If you pass a regular expression, it must have the global flag.
Check the result
Expect item item followed by the original a.b a.b. Test a replacement containing a dollar sign if user input supplies the replacement text.
Sources
Reference documentation for the behavior used in this guide: