136

I need to replace all the string in a variable.

var a = "::::::";
a = a.replace(":", "hi");
console.log(a);

The above code replaces only the first string i.e..hi:::::: I used replaceAll but it's not working.

leonheess
  • 16,068
  • 14
  • 77
  • 112
Vishnu Chid
  • 1,463
  • 2
  • 10
  • 10

2 Answers2

239

Update: All recent versions of major browsers, as well as NodeJS 15+ now support replaceAll

Original:

There is no replaceAll in JavaScript: the error console was probably reporting an error.

Instead, use the /g ("match globally") modifier with a regular expression argument to replace:

const a = "::::::";
const replaced = a.replace(/:/g,"hi");
console.log(replaced);

The is covered in MDN: String.replace (and elsewhere).

Janne Annala
  • 25,928
  • 8
  • 31
  • 41
  • why do this code not working var a=":):)::::"; a = a.replace(/:)/g,"hi"); alert(a); – Vishnu Chid Nov 12 '12 at 08:09
  • 4
    @VishnuChid Because `/:)/g` is an invalid regular expression literal (it will result in a SyntaxError due to the "extra" closing parenthesis). Try `/:\)/g` instead. Please read the error messages and be precise about error messages - "not working" and "doesn't work" are very vague. –  Nov 12 '12 at 08:10
  • i get SyntaxError: missing ) after argument list for /:\)/g , please help , and what is the topic i should read about for using slashes ?? – Vishnu Chid Nov 12 '12 at 08:16
  • 1
    http://www.regular-expressions.info/ – Barmar Nov 12 '12 at 08:17
  • 88
    It's been many years since this answer was made, and replaceAll is now included in MDN documentation and the ECMA-262 (2021) spec, but replaceAll still isn't widely available in all browsers. Firefox is including it starting with version 77. Hopefully this update saves someone a few minutes of confusion. – jejese May 26 '20 at 19:20
  • 5
    What's that then? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll – mgPePe Jul 17 '20 at 16:38
  • 3
    @mgPePe read comment above. – Alex78191 Jul 31 '20 at 23:00
  • 18
    Node JS does NOT support replaceAll !!! – Rahul Ranjan Dec 03 '20 at 13:53
  • 2
    show this answer when testers don't believe devs "It's been many years since this answer was made, and replaceAll is now included in MDN documentation and the ECMA-262 (2021) spec, but replaceAll still isn't widely available in all browsers. Firefox is including it starting with version 77. Hopefully this update saves someone a few minutes of confusion." ... – Tushar Pandey Jan 29 '21 at 11:39
  • 1
    ```String.prototype.replaceAll = (search, replacement) => { const target = this; return target.split(search).join(replacement); }; ``` – Barkles Jul 01 '21 at 03:39
  • 6
    As of node 15, node supports replaceAll. – DORRITO Jul 06 '21 at 23:01
82

There is no replaceAll function in JavaScript.

You can use a regex with a global identifier as shown in pst's answer:

a.replace(/:/g,"hi");

An alternative which some people prefer as it eliminates the need for regular expressions is to use JavaScript's split and join functions like so:

a.split(":").join("hi");

It is worth noting the second approach is however slower.

Mitch Satchwell
  • 4,770
  • 2
  • 24
  • 31
  • 6
    update 2021: `String.prototype.replaceAll()` is now a valid function in JS but has no support in Node yet. Example: ` let p = 'The dog ate my homework. Bad dog.'; p.replaceAll('dog', 'goat'); console.log(p); // 'The goat ate my homework. Bad goat.' ` – Shem Muthanga Apr 29 '21 at 21:12
  • 3
    https://2ality.com/2019/12/string-prototype-replaceall.html introduced in ES2021 – Max Carroll Apr 30 '21 at 12:55
  • 1
    Node 15 now supports replaceAll. – DORRITO Jul 06 '21 at 23:02
  • https://stackoverflow.com/questions/1144783/how-do-i-replace-all-occurrences-of-a-string-in-javascript clearly shows that there is too a 'replaceAll ' in JavaScript – TV-C-1-5 Aug 27 '22 at 02:18