TIM*_*MEX 117 javascript string node.js
s = 'hello %s, how are you doing' % (my_name)
Run Code Online (Sandbox Code Playgroud)
这就是你在python中的表现.你怎么能在javascript/node.js中做到这一点?
Sri*_*har 355
使用Node.js v4,您可以使用ES6的模板字符串
var my_name = 'John';
var s = `hello ${my_name}, how are you doing`;
console.log(s); // prints hello John, how are you doing
Run Code Online (Sandbox Code Playgroud)
你需要在反引号中包裹字符串`
而不是'
Fel*_*ing 43
如果你想要类似的东西,你可以创建一个函数:
function parse(str) {
var args = [].slice.call(arguments, 1),
i = 0;
return str.replace(/%s/g, () => args[i++]);
}
Run Code Online (Sandbox Code Playgroud)
用法:
s = parse('hello %s, how are you doing', my_name);
Run Code Online (Sandbox Code Playgroud)
这只是一个简单的例子,不考虑不同类型的数据类型(如%i等)或转义%s.但我希望它能给你一些想法.我很确定那里还有一些库提供了这样的功能.
Jim*_*ert 38
util.format这样做.
它将是v0.5.3的一部分,可以像这样使用:
var uri = util.format('http%s://%s%s',
(useSSL?'s':''), apiBase, path||'/');
Run Code Online (Sandbox Code Playgroud)
And*_*510 36
因此,node.js >4.0它与ES6标准更加兼容,其中字符串操作得到了极大的改进.
原始问题的答案可以简单如下:
var s = `hello ${my_name}, how are you doing`;
// note: tilt ` instead of single quote '
Run Code Online (Sandbox Code Playgroud)
在字符串可以传播多行的地方,它使模板或HTML/XML过程变得非常容易.关于它的更多细节和更多功能:模板文字是 mozilla.org 上的字符串文字.
Ter*_*nce 32
如果您使用的是ES6,则应使用模板文字.
//you can do this
let sentence = `My name is ${ user.name }. Nice to meet you.`
Run Code Online (Sandbox Code Playgroud)
在这里阅读更多内容:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
Mer*_*kos 11
去做
s = 'hello ' + my_name + ', how are you doing'
Run Code Online (Sandbox Code Playgroud)
我写了一个函数来精确解决这个问题。
第一个参数是要参数化的字符串。你应该把你的变量放在这个字符串中,就像这样的格式"%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];
});
}
Run Code Online (Sandbox Code Playgroud)
例子
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"
Run Code Online (Sandbox Code Playgroud)
如果该字符串中的变量位置发生变化,该函数也支持它,而无需更改函数参数。
parameterizedString("i have %s2 %s1 and %s4 %s3.", "books", 5, "pencils", "6");
// returns "i have 5 books and 6 pencils."
Run Code Online (Sandbox Code Playgroud)
扩展String.prototype或使用ES2015 模板文字的几种方法。
var result = document.querySelector('#result');
// -----------------------------------------------------------------------------------
// Classic
String.prototype.format = String.prototype.format ||
function () {
var args = Array.prototype.slice.call(arguments);
var replacer = function (a){return args[a.substr(1)-1];};
return this.replace(/(\$\d+)/gm, replacer)
};
result.textContent =
'hello $1, $2'.format('[world]', '[how are you?]');
// ES2015#1
'use strict'
String.prototype.format2 = String.prototype.format2 ||
function(...merge) { return this.replace(/\$\d+/g, r => merge[r.slice(1)-1]); };
result.textContent += '\nHi there $1, $2'.format2('[sir]', '[I\'m fine, thnx]');
// ES2015#2: template literal
var merge = ['[good]', '[know]'];
result.textContent += `\nOk, ${merge[0]} to ${merge[1]}`;Run Code Online (Sandbox Code Playgroud)
<pre id="result"></pre>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
187522 次 |
| 最近记录: |