Replace method doesn’t work

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, "'");


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.

replace return the resulting string

str = str.replace(/["']/, '');

The OP doesn’t say why it isn’t working, but there seems to be problems related to the encoding of the file. If I have an ANSI encoded file and I do:

var s = "“This is a test” ‘Another test’";
s = s.replace(/[“”]/g, '"').replace(/[‘’]/g,"'");

I get:

"This is a test" "Another test"

I converted the encoding to UTF-8, fixed the smart quotes (which broke when I changed encoding), then converted back to ANSI and the problem went away.

Note that when I copied and pasted the double and single smart quotes off this page into my test document (ANSI encoded) and ran this code:

var s = "“This is a test” ‘Another test’";
for (var i = 0; i < s.length; i++) {
    document.writeln(s.charAt(i) + '=' + s.charCodeAt(i));

I discovered that all the smart quotes showed up as ? = 63.

So, to the OP, determine where the smart quotes are originating and make sure they are the character codes you expect them to be. If they are not, consider changing the encoding of the source so they arrive as “ = 8220, ” = 8221, ‘ = 8216 and ’ = 8217. Use my loop to examine the source, if the smart quotes are showing up with any charCodeAt() values other than those I’ve listed, replace() will not work as written.

Read More:   No colon after property name in object declaration, is it valid? [duplicate]

To replace all regular quotes with smart quotes, I am using a similar function. You must specify the CharCode as some different computers/browsers default settings may identify the plain characters differently (“”,”,’,’).

Using the CharCode with call the ASCII character, which will eliminate the room for error across different browsers, and operating systems. This is also helpful for bilingual use (accents, etc.).

To replace smart quotes with SINGLE QUOTES

function unSmartQuotify(n){
    var name = n;
    var apos = String.fromCharCode(39);
    while (n.indexOf("'") > -1)
        name = name.replace("'" , apos);
    return name;

To find the other ASCII values you may need. Check here.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts