简体   繁体   中英

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" . 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" .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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