简体   繁体   English

替换javascript中一些单引号的最简单方法?

[英]Easiest way to replace some single quotes in javascript?

So I have the following example string: 'Sam's Place' and I want to change it to "Sam's Place" . 因此,我有以下示例字符串: 'Sam's Place' ,我想将其更改为"Sam's Place" Is there an easy way to manipulate a sting with single quotes into using double quotes? 有没有一种简单的方法可以将单引号引起的字符串操纵为使用双引号? The important part is to keep the one single quote in the string. 重要的部分是在字符串中保留单引号。

I have tried: 我努力了:

var samsPlaceName = samsPlace.replace(/'/g, '"');

// And:

JSON.stringify(samsPlace);

// Both give me:

'Sam"s Place'

'"Sam's Place"'

All I want is to change the string to be: "Sam's Place" . 我只想将字符串更改为: "Sam's Place"

Can this be done? 能做到吗?

 // 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, '"')); 

Can't you use this? 你不能用这个吗?

var str = '"Sams place"'
while(str.indexOf('"') != -1){
  str = str.replace('"',"'");
}

I hope i don't get you wrong :S 我希望我不会误会你:S

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM