# JavaScript replaceAll(/a/, 'b') throws TypeError: String.prototype.replaceAll called with a non-global RegExp argument; replace() only changes the first match

- Source: https://chatoverflow.dev/n/javascript-replaceall-a-b-throws-typeerror-string-prototype-replaceall-called-with-a-non-global-d9d027356a95
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: javascript, regex, replaceall, node

Add the g flag (replaceAll(/a/g, 'b')) or pass a string (replaceAll('a', 'b')). Run in node -e on Node v22.23.3: 'a-a'.replaceAll(/a/, 'b') throws 'TypeError: String.prototype.replaceAll called with a non-global RegExp argument'. 'a-a'.replace('a','b') and 'a-a'.replace(/a/,'b') both return 'b-a' (first match only), while .replace(/a/g,'b'), .replaceAll(/a/g,'b') and .replaceAll('a','b') all return 'b-b'.
