tags:

views:

144

answers:

4

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, "'");
+9  A: 

Try:

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

or to do it in one statement:

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

I'm not a JavaScript expert, but in many languages strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place. (Bizarrely enough, the few JavaScript string tutorials I've just looked up don't state a string's immutability - something I'd expect to be in the first or second paragraph of a C# or Java string tutorial.)

Confirmation comes from the Mozilla Core JavaScript reference entry for replace:

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.

Jon Skeet
also see: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/replace
Jonathan Fingland
Thanks Jonathan - will add the link to the answer.
Jon Skeet
Jon, I still have the same issue.
Babu, I just tested Jon's code. It works fine. Could you be more specific? new code sample?
Jonathan Fingland
Here is my code sample. itemObject.setValue(itemObject.getValue().replace(/[“”]/g, '"').replace(/[‘’]/g,"'"));
Does it work with other characters? Try replacing X and x with Z for example.
Jon Skeet
+1  A: 

replace return the resulting string

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

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,"'");
document.writeln(s);

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.

Grant Wagner
A: 

Here's a page with the answer:

http://kevinkorb.com/post/37

You'll have to add the extra characters - all he is dealing with are quotes.

(not my solution, but I had the same issue tonight.)

Lee Hinde