如何处理字符串中的撇号和引号转换

how to handle both apostrophes and quotation marks conversion in string

本文关键字:转换 字符串 何处理 处理      更新时间:2023-09-26

如何在字符串中同时处理撇号和引号转换

目前我只处理撇号

 var strname = data[i].name
strname = strname.replace("'","@");

以及以后如何将其替换回来strrename=strrename.replace("@","'");

我真的不确定你想在这里做什么,但如果你试图用@符号替换字符串中的单引号和双引号,那么这应该有效:

var strname = data[i].name.replace("'","@").replace("'"","@");

做两次替换既昂贵又不必要…

strname = strname.replace(/["']/g, "@");

读取https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions和https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace.

就像处理撇号一样。

strname.
    replace('"', 'what_you_want_quotes_to_be_replaced_with').
    replace("'", 'what_you_want_apostrophes_to_be_replaced_with');
strname = strname.replace("'","@");
strname = strname.replace("'"","@");