Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有定位占位符或等效占位符的字符串格式_Javascript_Python_String - Fatal编程技术网

Javascript 带有定位占位符或等效占位符的字符串格式

Javascript 带有定位占位符或等效占位符的字符串格式,javascript,python,string,Javascript,Python,String,我想在Javascript中使用位置占位符格式化字符串。例如,在Python中,我可以执行以下操作: st1 = 'name' st2 = 'javascript' sentence = "My %s is %s." %(st1, st2) print(sentence) 我会得到 My name is javascript. 作为输出。显然,它在javascript中不起作用(至少不完全相同),我知道使用replace var st1 = 'name'; var st2 = 'javasc

我想在Javascript中使用位置占位符格式化字符串。例如,在Python中,我可以执行以下操作:

st1 = 'name'
st2 = 'javascript'
sentence = "My %s is %s." %(st1, st2)
print(sentence)
我会得到

My name is javascript.
作为输出。显然,它在javascript中不起作用(至少不完全相同),我知道使用
replace

var st1 = 'name';
var st2 = 'javascript';
var sentence = "My %s is %s".replace('%s', st1);
console.log(sentence);
但这只会替换第一次出现的“%s”

var st1 = 'name';
var st2 = 'javascript';
var sentence = `My ${st1} is ${st2}`;
console.log(sentence);
它将替换句子中的两个字符串


它将替换句子中的两个字符串。

为什么不这样做:
“我的%s是%s”。替换('%s',st1)。替换('%s',st2)?在ES6中,可以使用字符串插值。有关详细信息:。为什么不这样做:
“我的%s是%s”。替换('%s',st1)。替换('%s',st2)?在ES6中,可以使用字符串插值。有关详细信息:。