[英]Easiest way to replace some single quotes in javascript?
因此,我有以下示例字符串: '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, '"'));
你不能用這個嗎?
var str = '"Sams place"'
while(str.indexOf('"') != -1){
str = str.replace('"',"'");
}
我希望我不會誤會你:S
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.