Replace method doesn't work

后端 未结 4 825
鱼传尺愫
鱼传尺愫 2020-11-21 05:35

I want to replace the smart quotes like , , and to regular quotes. Also, I wanted to replace the ©,

4条回答
  •  攒了一身酷
    2020-11-21 06:06

    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.

提交回复
热议问题