🌐
SPGuides
spguides.com β€Ί typescript-string-replaces-all-occurrences
TypeScript Replace All | TypeScript Replace All Occurrences in String
June 29, 2025 - The Typescript replaceAll method allows you to replace all occurrences of a specified substring within a string.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί typescript β€Ί typescript-string-replaceall-method
TypeScript String replaceAll method - GeeksforGeeks
July 19, 2024 - The TypeScript replaceAll method replaces all occurrences of a specified substring within a string with another substring. Unlike replace, which changes only the first match, replaceAll modifies every instance, supporting both strings and regular ...
Discussions

Replace all instances of character in string in typescript?
I'm trying to replace all full stops in an email with an x character - for example "my.email@email.com" would become "myxemail@emailxcom". Email is set to a string. My problem is it's not replacing... More on stackoverflow.com
🌐 stackoverflow.com
javascript - How do I replace all occurrences of a string?
Given a string: string = "Test abc test test abc test test test abc test test abc"; This seems to only remove the first occurrence of abc in the string above: string = string.replace('ab... More on stackoverflow.com
🌐 stackoverflow.com
How do I replace all occurrences of a string?
Given a string: string = "Test abc test test abc test test test abc test test abc"; This seems to only remove the first occurrence of abc in the string above: string = string.replace('ab... More on stackoverflow.com
🌐 stackoverflow.com
November 5, 2016
String.Replace only replaces first occurrence of matched string. How to replace *all* occurrences?
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Coming from other programming languages, String.replace() typically replaces all occurrences of matching strings. However, that is not the case with javascript/typescript. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Bobby Hadz
bobbyhadz.com β€Ί blog β€Ί typescript-string-replace-all-occurrences
Replace all occurrences of a String in TypeScript | bobbyhadz
February 27, 2024 - The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement. ... We used the g flag to replace all occurrences of the regex in the string and not just the first ...
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Global_Objects β€Ί String β€Ί replaceAll
String.prototype.replaceAll() - JavaScript - MDN Web Docs
The replaceAll() method of String values returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
🌐
Dirask
dirask.com β€Ί posts β€Ί TypeScript-replace-all-occurrences-of-string-1Aoqmp
TypeScript - replace all occurrences of string
const ESCAPE_EXPRESSION: RegExp = /([.*+?^=!:${}()|\[\]\/\\])/g; function escapeExpression(expressionText: string): RegExp { expressionText = expressionText.replace(ESCAPE_EXPRESSION, '\\$1'); return new RegExp(expressionText, 'g'); // global searching flag } function replaceAll(text: string, what: string, to: string): string { const whatExpression: RegExp = escapeExpression(what); return text.replace(whatExpression, to); } // Usage example: const inputText: string = 'This? is? my text.'; const searchText: string = 'is?'; const newText: string = '__'; const outputText: string = replaceAll(inputText, searchText, newText); console.log(outputText); // Output: Th__ __ my text. Note: escapeExpression function escapes all characters from regular expression pattern.
🌐
Webdevtutor
webdevtutor.net β€Ί blog β€Ί typescript-replace-all-occurrences-in-string
How to Replace All Occurrences in a String using TypeScript
By utilizing the replace() method in TypeScript along with regular expressions and the global flag 'g', you can easily replace all occurrences of a substring within a string.
🌐
Webdevtutor
webdevtutor.net β€Ί blog β€Ί typescript-replace-string-all
How to Replace All Occurrences of a String in TypeScript
In this example, the global regular ... approach to replace all instances of a string is by splitting the original string based on the substring to be replaced and then joining it back with the replacement string....
🌐
Webdevtutor
webdevtutor.net β€Ί blog β€Ί typescript-replace-all-values-in-string
How to Replace All Values in a String Using TypeScript
In this example, the regular expression /Hello/g is used to match all occurrences of "Hello" in the originalString and replace them with "Hi". Another method to replace all values in a string is by splitting the string based on the value to be replaced and then joining it back with the new value.
Find elsewhere
🌐
Webdevtutor
webdevtutor.net β€Ί blog β€Ί typescript-string-replace-all
Mastering TypeScript String Replace All: A Comprehensive Guide
The replace() method is a built-in function in JavaScript (and by extension, TypeScript) that replaces a specified substring with another substring or an empty string (''). ... const originalString = 'Hello World!'; const replacementString = 'Goodbye'; const replacedString = originalString...
Top answer
1 of 16
5259

As of August 2020: Modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 language specification.


For older/legacy browsers:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

Here is how this answer evolved:

str = str.replace(/abc/g, '');

In response to comment "what's if 'abc' is passed as a variable?":

var find = 'abc';
var re = new RegExp(find, 'g');

str = str.replace(re, '');

In response to Click Upvote's comment, you could simplify it even more:

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(find, 'g'), replace);
}

Note: Regular expressions contain special (meta) characters, and as such it is dangerous to blindly pass an argument in the find function above without pre-processing it to escape those characters. This is covered in the Mozilla Developer Network's JavaScript Guide on Regular Expressions, where they present the following utility function (which has changed at least twice since this answer was originally written, so make sure to check the MDN site for potential updates):

function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

So in order to make the replaceAll() function above safer, it could be modified to the following if you also include escapeRegExp:

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
2 of 16
2537

For the sake of completeness, I got to thinking about which method I should use to do this. There are basically two ways to do this as suggested by the other answers on this page.

Note: In general, extending the built-in prototypes in JavaScript is generally not recommended. I am providing as extensions on the String prototype simply for purposes of illustration, showing different implementations of a hypothetical standard method on the String built-in prototype.


Regular Expression Based Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

Split and Join (Functional) Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
};

Not knowing too much about how regular expressions work behind the scenes in terms of efficiency, I tended to lean toward the split and join implementation in the past without thinking about performance. When I did wonder which was more efficient, and by what margin, I used it as an excuse to find out.

On my Chrome Windows 8 machine, the regular expression based implementation is the fastest, with the split and join implementation being 53% slower. Meaning the regular expressions are twice as fast for the lorem ipsum input I used.

Check out this benchmark running these two implementations against each other.


As noted in the comment below by @ThomasLeduc and others, there could be an issue with the regular expression-based implementation if search contains certain characters which are reserved as special characters in regular expressions. The implementation assumes that the caller will escape the string beforehand or will only pass strings that are without the characters in the table in Regular Expressions (MDN).

MDN also provides an implementation to escape our strings. It would be nice if this was also standardized as RegExp.escape(str), but alas, it does not exist:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

We could call escapeRegExp within our String.prototype.replaceAll implementation, however, I'm not sure how much this will affect the performance (potentially even for strings for which the escape is not needed, like all alphanumeric strings).

🌐
Designcise
designcise.com β€Ί web β€Ί tutorial β€Ί how-to-replace-all-occurrences-of-a-word-in-a-javascript-string
How to Replace All Occurrences of a Word in a JavaScript String? - Designcise
January 29, 2021 - Let's assume we have the following ... String.prototype.replaceAll() Introduced in ES12, the replaceAll() method returns a new string with all matches replaced by the specified replacement....
🌐
TutorialsPoint
tutorialspoint.com β€Ί typescript β€Ί typescript_string_replace.htm
TypeScript - String replace()
It simply returns a new changed string. var re = /apples/gi; var str = "Apples are round, and apples are juicy."; var newstr = str.replace(re, "oranges"); console.log(newstr) On compiling, it will generate the same code in JavaScript.
Top answer
1 of 16
5259

As of August 2020: Modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 language specification.


For older/legacy browsers:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

Here is how this answer evolved:

str = str.replace(/abc/g, '');

In response to comment "what's if 'abc' is passed as a variable?":

var find = 'abc';
var re = new RegExp(find, 'g');

str = str.replace(re, '');

In response to Click Upvote's comment, you could simplify it even more:

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(find, 'g'), replace);
}

Note: Regular expressions contain special (meta) characters, and as such it is dangerous to blindly pass an argument in the find function above without pre-processing it to escape those characters. This is covered in the Mozilla Developer Network's JavaScript Guide on Regular Expressions, where they present the following utility function (which has changed at least twice since this answer was originally written, so make sure to check the MDN site for potential updates):

function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

So in order to make the replaceAll() function above safer, it could be modified to the following if you also include escapeRegExp:

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
2 of 16
2537

For the sake of completeness, I got to thinking about which method I should use to do this. There are basically two ways to do this as suggested by the other answers on this page.

Note: In general, extending the built-in prototypes in JavaScript is generally not recommended. I am providing as extensions on the String prototype simply for purposes of illustration, showing different implementations of a hypothetical standard method on the String built-in prototype.


Regular Expression Based Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

Split and Join (Functional) Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
};

Not knowing too much about how regular expressions work behind the scenes in terms of efficiency, I tended to lean toward the split and join implementation in the past without thinking about performance. When I did wonder which was more efficient, and by what margin, I used it as an excuse to find out.

On my Chrome Windows 8 machine, the regular expression based implementation is the fastest, with the split and join implementation being 53% slower. Meaning the regular expressions are twice as fast for the lorem ipsum input I used.

Check out this benchmark running these two implementations against each other.


As noted in the comment below by @ThomasLeduc and others, there could be an issue with the regular expression-based implementation if search contains certain characters which are reserved as special characters in regular expressions. The implementation assumes that the caller will escape the string beforehand or will only pass strings that are without the characters in the table in Regular Expressions (MDN).

MDN also provides an implementation to escape our strings. It would be nice if this was also standardized as RegExp.escape(str), but alas, it does not exist:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

We could call escapeRegExp within our String.prototype.replaceAll implementation, however, I'm not sure how much this will affect the performance (potentially even for strings for which the escape is not needed, like all alphanumeric strings).

🌐
Sentry
sentry.io β€Ί sentry answers β€Ί javascript β€Ί how do i replace all occurrences of a string in javascript?
How do I Replace all Occurrences of a String in JavaScript? | Sentry
For example, you want to kebab case a string of words: ... You need to replace all the empty spaces with a -. How do you do this? The simplest and best way to replace multiple occurrences of a substring in a string is to use the replaceAll method.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί typescript β€Ί typescript-string-replace-method
TypeScript String replace() Method - GeeksforGeeks
July 19, 2024 - Below example illustrate the String replace() method in TypeScriptJS: This example demonstrates how to replace a simple substring with another string. JavaScript Β· const str: string = "Geeksforgeeks is a great platform."; const newStr: string ...
🌐
Dmitri Pavlutin
dmitripavlutin.com β€Ί replace-all-string-occurrences-javascript
3 Ways To Replace All String Occurrences in JavaScript
January 27, 2023 - Finally, the method string.replaceAll(search, replaceWith) replaces all appearances of search string with replaceWith. ... Open the demo. 'duck duck go'.replaceAll(' ', '-') replaces all occurrences of ' ' string with '-'.
🌐
Alexanderkaran
blog.alexanderkaran.com β€Ί replace-vs-replace-all
Replace vs Replace All Comparison - Alexander Karan's Blog
October 21, 2024 - Learn how `replace` and `replaceAll` functions differ, and discover the benefits of using `replaceAll` for global string replacements