---
title: "How do you replace all occurrences of a string in JavaScript?"
description: "Use String.prototype.replaceAll(), or replace() with a global regular expression, to substitute every match in a string."
url: "https://bugsee.com/answers/javascript/replace-all-occurrences-of-a-string/"
---

# How do you replace all occurrences of a string in JavaScript?

Call `str.replaceAll(search, replacement)`. On older runtimes use `str.replace(/search/g, replacement)`.

```js
"a-b-c".replaceAll("-", "+");   // "a+b+c"
"a-b-c".replace(/-/g, "+");     // "a+b+c" (works everywhere)

// replace() with a plain string only changes the first match:
"a-b-c".replace("-", "+");      // "a+b-c"
```

If the text you search for comes from user input, escape it before turning it into a regular expression, or use `replaceAll` with a plain string so special characters such as `.` and `*` are treated literally.

## Related questions

- [How do you format a date in JavaScript?](https://bugsee.com/answers/javascript/format-a-date/)
