Are you getting ReplaceAll Is Not a Function Error from your JavaScript code block? The clear-cut answer is that either you are using an older version of the web browser or you are using the wrong implementation of the JavaScript ReplaceAll method.
Table of Contents
There was no replaceAll method or function in JavaScript before MDN ES2012 or ES12 were released. But today, the replaceAll function is included in every latest browser as per MDN documentation. For the latest browsers, you don’t have to worry about this method. But the error “ReplaceAll Is Not a Function” is still a valid concern for older browsers.
Firefox is including it from version 77, and Google Chrome starts from version 85. The documentation is also available in MDN now.
But if you are using the latest web browser versions and getting the error ReplaceAll Is Not a Function, then you are definitely using the wrong implementation of this JS ReplaceAll method. Please continue with us. We are going to explain three easy ways to overcome this error, and we will also discuss the latest ReplaceAll JS function in more detail.
Why we Get the JavaScript ReplaceAll Is Not a Function Error?
I am going to remind you again. First of all, note clearly that JavaScript ReplaceAll is not a function in earlier versions of web browsers. Only the replace method is available. But now each and every web browser has introduced this JS function aside from the earlier only JS replace function.
Don’t be confused about the Replace and ReplaceAll JS functions. The replace function is available in JS, but the replaceAll function was introduced later on.
So, if you are trying to use the ReplaceAll function for old browsers, you are definitely facing the error ReplaceAll Is Not a Function error.
How to Fix the Error JS ReplaceAll is not a Function?
Before the fixation, we need your answers to the following questions:
- Are you developing your JS code for older web browser versions?
- Are you developing your JS code for newer web browser versions?
- Or for both?
If you have the requirement to give older versions support, then we have the fixes now. Use any one of them to resolve the error “ReplaceAll Is Not a Function”.
Fix No. 1: Use JavaScript Split and Join Functions Together:
According to this approach, to resolve the error, you have to combine both the JS functions Split and Join to fix ReplaceAll Is Not a Function error. So, before going into the detailed code example, we would like to give you an overview of these two methods.
The JS Split() method splits a string and returns an array of substrings. This method does not change the original string.
The Join() method is an array method. Here, all of the elements in the array are concatenated and returned as a new string, either with commas or a provided separator string in between.
Now look at the below code. We are going to replace all spaces ‘ ‘ with hyphens ‘-‘ in ‘JavaScript ReplaceAll Is Not a Function Error’ string:
<script>
var lookFor = ' ';
var replaceWith = '-';
//Combining both split and join method to
//Replace All Instances of a String in JS
var replacedString = 'JavaScript ReplaceAll Is Not a Function Error'.split(lookFor).join(replaceWith);
console.log(replacedString);
</script>
Output
Code Explanation
‘JavaScript ReplaceAll Is Not a Function Error’.split(‘ ‘) splits the string into pieces: [‘JavaScript’,’ReplaceAll’,’Is’,’Not’,’a’,’Function’,’Error’].
Then the pieces [‘JavaScript’,’ReplaceAll’,’Is’,’Not’,’a’,’Function’,’Error’].join(‘-‘) are joined by inserting ‘-‘ in between them, which results a new string ‘JavaScript-ReplaceAll-Is-Not-a-Function-Error’.
Make a Generic Version to Extend JS Replace Method
Here we are going to make a generic method based on the same combination of Split & Join method. So that you can reuse the code in your all upcoming projects. It will surely increase your code manageability as well.
<script>
function replaceAll(string, lookFor, replaceWith) {
return string.split(lookFor).join(replaceWith);
}
//javascript string replace all
console.log(replaceAll('xx yy xx yy', 'yy', 'xx'));// Output: 'xx xx xx xx'
console.log(replaceAll('11 11 22 22', '2', '1'));// Output: '11 11 11 11'
console.log(replaceAll('javaScript CSS HTML', ' ', ','));// Output: 'javaScript,CSS,HTML'
console.log(replaceAll('We Love JavaScript Also', 'Java', 'Type'));// Output: 'We Love TypeScript Also'
</script>
Output
Fix No. 2: Use replace() with a Global Regular Expression
As we all know from our above discussion, the JS replace() method searches a string or a set of characters for a value, but it replaces only the first string, not all. To address this issue, we can use the JS replace() method with a regular expression along with the global (“g”) flag set, which has strong support for all ES versions and browsers.
Follow one of two methods while using the replace() method with a global regular expression to fix ReplaceAll Is Not a Function Javascript error:
If you are using regular expression literals, then add the global “g” flag set at the end of the regular expression literal.
If you are using a regular expression constructor, add ‘g’ as the second argument.
Let’s replace all occurrences of ‘ ‘ with ‘-‘, like in our first example:
<script>
// Use g global tag at the end of the literal
var lookForRegExp = / /g;
var replaceWith = '-';
// This regular expression is going to
// How do I replace all occurrences of a string in JavaScript?
// not the first occurrence only
var replacedString = 'JavaScript ReplaceAll Is Not a Function Error'.replace(lookForRegExp, replaceWith);
console.log(replacedString); // 'JavaScript-ReplaceAll-Is-Not-a-Function-Error'
</script>
Output
Code Explanation
The regex / /g we used here is to match the empty space with the global flag. Basically, we are using the replace function to replace all occurrences. It means we are replacing all empty spaces with ‘-‘ within the sentence. Later on we just print the output in JS console log.
Another Code Example
<script>
var string = "aa bb aa bb";
var replacedString1 = string.replace(/b/g,"a");
var replacedString2 = string.replace(/bb/g,"aa");
//You can use single charater or multiple charaters
console.log(replacedString1); // Output: aa aa aa aa
console.log(replacedString2); // Output: aa aa aa aa
</script>
<script>
var string = "aa bb aa bb";
// Use 'i' tag to make your search case insensitive
// javascript string replace all
var replacedString1 = string.replace(/B/gi,"a");
var replacedString2 = string.replace(/BB/gi,"aa");
//You can use single charater or multiple charaters
console.log(replacedString1); // Output: aa aa aa aa
console.log(replacedString2); // Output: aa aa aa aa
</script>
Fix No. 3: Upgrade Your Browser Version
If you are not comfortable with our above two solutions, then the only alternative way to find and replace all strings in JS is to upgrade your browser to the latest version and use the ReplaceAll() method. This method is only supported by the latest browsers.
So, based on the above discussion, we are concluding that if you need older browser support along with or without the new browser support, you can use our Fix No. 1 or Fix No. 2 to implement javascript string replace all method.
But if you don’t need older browser support, then it’s recommended to use the JavaScript ReplaceAll function to fix the Javascript ReplaceAll Is Not a Function error.
JavaScript Replace All Function Syntax
The basic syntax is:
str.replaceAll(pattern, replacement)
JavaScript Replace All Function Parameter
Here str is your working string, and you are going to apply the JavaScript ReplaceAll function to it.
The first parameter pattern can hold a substring or a regular expression that you want to change or replace with something else.
The second parameter is replacement. It can hold a substring or a function. It will replace the pattern.
JavaScript String replaceAll() Method Code Example
<script>
var working_String = "With JavaScript, dynamic web content can be created, enhancing web user experiences.";
let pattern = "web";
let replacement = "online";
let replaced_string = working_String.replaceAll(pattern,replacement);
console.log(replaced_string);
</script>
Output – Replace All String JavaScript
JS Replace vs. ReplaceAll Method
The whole article will be meaningless if we do not discuss the difference between the JS Replace and ReplaceAll methods. Let’s take a look at the difference in the following table:
JS Replace | JS ReplaceAll |
---|---|
The JS replace() method replaces only the first occurrence. | The JS replaceAll() method replaces all instances of the string. |
Browser compatibility: Chrome 1, Firefox 1, Safari 1, Edge 12, Opera 4. | Browser compatibility: Chrome 85, Firefox 77, Safari 13.1, Edge 85, Opera 71. |
Knowing the differences, your skills will be enhanced. And you can choose one over the other based on your requirements.
Conclusion
OK. Here we have discussed 3 proven fixes to solve the “JavaScript ReplaceAll Is Not a Function” error.
The first fix is to replace all occurrences by splitting a given string into chunks by the search string and then joining back the string to place the replace string in between the chunks. It’s not an inbuilt JS way, but rather a code hack by combining both split and join JS methods.
The second approach is to use a regular expression with the global flag enabled.
And the third one is to use the JavaScript ReplaceAll method. It works well in most modern browsers but is not compatible with older versions.
What are the other possible ways to replace all string occurrences? If you know, please share with our readers by leaving a comment below!
🏋️♂️ Discover Code Blocks From 20+ yrs JS Expert
💥 Asp.net C# Developer
🏆 Solution Architect
👨✈️ Database Administrator
📢 Speaker
🎓 MCTS since 2009
Leave a Reply