Replace method doesn’t work


str = str.replace(/[“”]/g, '"');
str = str.replace(/[‘’]/g, "'");

or to do it in one statement:

str = str.replace(/[“”]/g, '"').replace(/[‘’]/g,"'");

In JavaScript (as in many other languages) strings are immutable – string “replacement” methods actually just return the new string instead of modifying the string in place.

The MDN JavaScript reference entry for replace states:

Returns a new string with some or all matches of a pattern replaced by a replacement.

This method does not change the String object it is called on. It simply returns a new string.

Leave a Comment