s = 'hello %s, how are you doing' % (my_name)
在python中就是这么做的。如何在javascript/node.js中做到这一点?
s = 'hello %s, how are you doing' % (my_name)
在python中就是这么做的。如何在javascript/node.js中做到这一点?
当前回答
我写了一个函数来精确地解决这个问题。
第一个参数是要参数化的字符串。你应该把你的变量放在这个字符串中,就像这样“%s1, %s2,…”% s12”。
其他参数分别是该字符串的参数。
/***
* @example parameterizedString("my name is %s1 and surname is %s2", "John", "Doe");
* @return "my name is John and surname is Doe"
*
* @firstArgument {String} like "my name is %s1 and surname is %s2"
* @otherArguments {String | Number}
* @returns {String}
*/
const parameterizedString = (...args) => {
const str = args[0];
const params = args.filter((arg, index) => index !== 0);
if (!str) return "";
return str.replace(/%s[0-9]+/g, matchedStr => {
const variableIndex = matchedStr.replace("%s", "") - 1;
return params[variableIndex];
});
}
例子
parameterizedString("my name is %s1 and surname is %s2", "John", "Doe");
// returns "my name is John and surname is Doe"
parameterizedString("this%s1 %s2 %s3", " method", "sooo", "goood");
// returns "this method sooo goood"
如果变量位置在字符串中改变了,这个函数也支持它而不改变函数参数。
parameterizedString("i have %s2 %s1 and %s4 %s3.", "books", 5, "pencils", "6");
// returns "i have 5 books and 6 pencils."
其他回答
如果你使用的是ES6,你应该使用模板文字。
//you can do this
let sentence = `My name is ${ user.name }. Nice to meet you.`
点击此处阅读更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
这样做:
s = 'hello ' + my_name + ', how are you doing'
更新
使用ES6,你也可以这样做:
s = `hello ${my_name}, how are you doing`
var user = "your name";
var s = 'hello ' + user + ', how are you doing';
我写了一个函数来精确地解决这个问题。
第一个参数是要参数化的字符串。你应该把你的变量放在这个字符串中,就像这样“%s1, %s2,…”% s12”。
其他参数分别是该字符串的参数。
/***
* @example parameterizedString("my name is %s1 and surname is %s2", "John", "Doe");
* @return "my name is John and surname is Doe"
*
* @firstArgument {String} like "my name is %s1 and surname is %s2"
* @otherArguments {String | Number}
* @returns {String}
*/
const parameterizedString = (...args) => {
const str = args[0];
const params = args.filter((arg, index) => index !== 0);
if (!str) return "";
return str.replace(/%s[0-9]+/g, matchedStr => {
const variableIndex = matchedStr.replace("%s", "") - 1;
return params[variableIndex];
});
}
例子
parameterizedString("my name is %s1 and surname is %s2", "John", "Doe");
// returns "my name is John and surname is Doe"
parameterizedString("this%s1 %s2 %s3", " method", "sooo", "goood");
// returns "this method sooo goood"
如果变量位置在字符串中改变了,这个函数也支持它而不改变函数参数。
parameterizedString("i have %s2 %s1 and %s4 %s3.", "books", 5, "pencils", "6");
// returns "i have 5 books and 6 pencils."
注意,从2015年开始,模板只使用反引号
https://stackoverflow.com/a/37245773/294884
let a = `hello ${name}` // NOTE!!!!!!!! ` not ' or "
注意,这是一个反引号,而不是引号。
如果你想要类似的东西,你可以创建一个函数:
function parse(str) {
var args = [].slice.call(arguments, 1),
i = 0;
return str.replace(/%s/g, () => args[i++]);
}
用法:
s = parse('hello %s, how are you doing', my_name);
这只是一个简单的示例,没有考虑不同类型的数据类型(如%i等)或%s的转义。但我希望这能给你们一些启发。我很确定也有库提供了类似的功能。