Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript .replaceAll() is not a function type error

The documentation page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:"; let newstring = string.replaceAll(":insertx:", 'hello!');

When I run this, I receive Uncaught TypeError: string.replaceAll is not a function. Maybe I'm misunderstanding what a prototype is, but the function appears to be a string method that is available for use.

I'm using Chrome.

like image 287
pyknight202 Avatar asked Jul 09 '20 23:07

pyknight202


People also ask

What can I use instead of replaceAll in Javascript?

To make the method replace() replace all occurrences of the pattern you have to enable the global flag on the regular expression: Append g after at the end of regular expression literal: /search/g. Or when using a regular expression constructor, add 'g' to the second argument: new RegExp('search', 'g')

Can I use replaceAll Javascript?

replaceAll() The replaceAll() method 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.

What does in replaceAll () mean in Java?

The replaceAll() method replaces each substring of this string that matches the given regular expression with the given replacement.

How do I use replaceAll in TypeScript?

To replace all occurrences of a string in TypeScript, use the replace() method, passing it a regular expression with the g (global search) flag. For example, str. replace(/old/g, 'new') returns a new string where all occurrences of old are replaced with new .


2 Answers

Use replace with a regular expression with the global modifier for better browser support. (Check the browser compatibility table on MDN to see which version of each browser started supporting the replaceAll method.)

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:"; let newstring = string.replace(/:insertx:/g, 'hello!'); console.log(newstring);

For a more generic solution, we can escape regular expression metacharacters and use the RegExp constructor. You could also add the function to String.prototype as a polyfill.

(It is necessary to escape the string to replace so that characters that have special meanings in regular expressions will be interpreted literally, e.g. . will refer only to actual dots rather than any character.)

//Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions function escapeRegExp(string) {   return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } function replaceAll(str, match, replacement){    return str.replace(new RegExp(escapeRegExp(match), 'g'), ()=>replacement); }  console.log(replaceAll('a.b.c.d.e', '.', '__')); console.log(replaceAll('a.b.c.d.e', '.', '$&'));

A specification-compliant shim can be found here.

like image 53
Unmitigated Avatar answered Sep 19 '22 16:09

Unmitigated


.replaceAll will be available starting on Chrome 85. The current version is 83.

If you download Google Chrome Canary (which is on version 86), you'll be able to see that your code runs fine. Firefox is on version 78, and since .replaceAll has been available starting version 77, it works there too. It will work on current Safari as well. Microsoft Edge has it as unsupported.

You'll find supported browser versions at the bottom of the article in your question.

like image 38
MattDiamant Avatar answered Sep 18 '22 16:09

MattDiamant