Replace method doesn't work

后端 未结 4 988
鱼传尺愫
鱼传尺愫 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.

提交回复
热议问题