Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace method doesn't work

Tags:

javascript

I want to replace the smart quotes like , , and to regular quotes. Also, I wanted to replace the ©, ® and . I used the following code. But it doesn't help. Kindly help me to resolve this issue.

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

1 Answers

Use:

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.

like image 137
Jon Skeet Avatar answered Sep 13 '25 10:09

Jon Skeet