Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why doesn't NodeJS support String.prototype.replaceAll?

Tags:

node.js

v8

This string method exists in browserland, but not in Node. Why not? It's neither brand-new nor exotic.

  • replaceAll is part of ECMA-262 src
  • replaceAll is described in detail on the V8 blog (added in v8.1) src
  • NodeJS upgraded to V8 v8.1 with NodeJS v14.0 src

And yet:

$ node
Welcome to Node.js v14.13.1.
Type ".help" for more information.
> 'asdf'.replaceAll
undefined

I'm not asking for polyfills; I can do a regex or loop over single-instance replace. I'm trying to figure out why this seemingly unremarkable feature doesn't exist in NodeJS, even though it seems to be available in just about every other JS environment, and even though all the docs suggest to me that it should exist.

like image 772
Tom Avatar asked Apr 04 '21 02:04

Tom


People also ask

Does Nodejs support replaceAll?

Nodejs v14 has V8 8.4 But, replaceAll was added to V8 8.5. Nodejs v15 has V8 8.6 and thus that's the first nodejs version where you can use . replaceAll() .

Why replaceAll is not working in JS?

The "replaceAll" is not a function error occurs when we call the replaceAll() method on a value that is not of type string, or in a browser that doesn't support it. To solve the error, only call the replaceAll() method on strings in supported browsers.

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 do the replaceAll () do?

The replaceAll() method replaces each substring that matches the regex of the string with the specified text.

What is the use of replaceAll() method in JavaScript?

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. The original string is left unchanged. JavaScript Demo: String.replaceAll ()

How do you replace a string with a pattern in JavaScript?

String.prototype.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. The original string is left unchanged.

How to fix JavaScript replaceAll() is not a function error?

In this article, we’ll look at how to fix the JavaScript .replaceAll () is not a function error in some browsers. To fix the JavaScript .replaceAll () is not a function error in some browsers, we can add a polyfill to make the method available in all browsers.


1 Answers

Nodejs v14 has V8 8.4 But, replaceAll was added to V8 8.5. Nodejs v15 has V8 8.6 and thus that's the first nodejs version where you can use .replaceAll(). You can see this all described in the nodejs v15 release notes or in this specific change report.

like image 95
jfriend00 Avatar answered Oct 19 '22 02:10

jfriend00