所以我有以下示例字符串:'Sam's Place'
,我想将其更改为“Sam's Place”
。有没有一种简单的方法可以将单引号的字符串转换为双引号?重要的部分是在字符串中保留一个单引号。
我已经尝试过:
var samsPlaceName = samsPlace.replace(/'/g, '"');
// And:
JSON.stringify(samsPlace);
// Both give me:
'Sam"s Place'
'"Sam's Place"'
我想要的只是将字符串更改为:“Sam's Place”
。
这可以做到吗?
最佳答案
// assume you have a string
var samsPlace = "'Sam's Place'";
console.log(samsPlace);
// replace the single quote at the beginning and end of string with double quote by
// using anchors where ^ denotes BOS (beginning of string) and $ denotes EOS (end of
// string)
console.log(samsPlace.replace(/^'|'$/g, '"'));
关于javascript - 在javascript中替换一些单引号的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45462976/