Replace method doesn't work

后端 未结 4 827
鱼传尺愫
鱼传尺愫 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 05:47

    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.

    0 讨论(0)
  • 2020-11-21 06:02

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 06:11

    replace return the resulting string

    str = str.replace(/["']/, '');
    
    0 讨论(0)
提交回复
热议问题