How to put variables in javascript strings? (Node.js)

s = 'hello %s, how are you doing' % (my_name) 

How do you do this in python. How can you do this in javascript / node.js?

+105
javascript string
Oct 17 2018-11-11T00:
source share
14 answers

If you want to have something similar, you can create a function:

 function parse(str) { var args = [].slice.call(arguments, 1), i = 0; return str.replace(/%s/g, () => args[i++]); } 

Using:

 s = parse('hello %s, how are you doing', my_name); 

This is a simple example that does not take into account various types of data types (for example, %i , etc.) or escaping %s . But I hope this gives you some insight. I am sure that there are also libraries that provide such a function.

+41
Oct 17 2018-11-11T00:
source share

With Node.js v4 you can use ES6 String Templates

 var my_name = 'John'; var s = `hello ${my_name}, how are you doing`; console.log(s); // prints hello John, how are you doing 

You need to wrap the string in backtick ` instead of '

+319
Sep 21 '15 at 12:33
source share

util.format does this.

It will be part of v0.5.3 and can be used as follows:

 var uri = util.format('http%s://%s%s', (useSSL?'s':''), apiBase, path||'/'); 
+37
Oct 18 '11 at 17:16
source share

As with node.js >4.0 , it becomes more compatible with the ES6 standard, where string handling is greatly improved.

The answer to the original question may be as simple as:

 var s = `hello ${my_name}, how are you doing`; // note: tilt ` instead of single quote ' 

If a string can span multiple lines, this makes it easy to create HTML / XML templates or processes. In more detail and in more detail about it: Template literals are string literals on mozilla.org.

+36
May 11 '16 at 9:18
source share

if you are using ES6 you should use template literals.

 //you can do this let sentence = 'My name is ${ user.name }. Nice to meet you.' 

read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

+22
Feb 28 '18 at 16:37
source share

Do it

 s = 'hello ' + my_name + ', how are you doing' 
+11
Oct 17 2018-11-11T00:
source share

Try sprintf in JS or you can use this gist

+4
Oct. 17 2018-11-11T00:
source share

 const format = (...args) => args.shift().replace(/%([jsd])/g, x => x === '%j' ? JSON.stringify(args.shift()) : args.shift()) const name = 'Csaba' const formatted = format('Hi %s, today is %s and your data is %j', name, Date(), {data: {country: 'Hungary', city: 'Budapest'}}) console.log(formatted) 
+3
Jun 30 '17 at 11:09 on
source share

If you use node.js, console.log () accepts the string format as the first parameter:

  console.log('count: %d', count); 
+2
Oct 17 '11 at 10:43
source share

The bob.js framework also looks like something similar:

 var sFormat = "My name is {0} and I am version {1}.0."; var result = bob.string.formatString(sFormat, "Bob", 1); console.log(result); //output: //========== // My name is Bob and I am version 1.0. 
+2
Nov 02 '12 at 20:14
source share
 var user = "your name"; var s = 'hello ' + user + ', how are you doing'; 
+1
Oct 17 2018-11-11T00:
source share

Here is an example of a multiline string literal in Node.js.

 > let name = 'Fred' > tm = `Dear ${name}, ... This is to inform you, ${name}, that you are ... IN VIOLATION of Penal Code 64.302-4. ... Surrender yourself IMMEDIATELY! ... THIS MEANS YOU, ${name}!!! ... ... ` 'Dear Fred,\nThis is to inform you, Fred, that you are\nIN VIOLATION of Penal Code 64.302-4.\nSurrender yourself IMMEDIATELY!\nTHIS MEANS YOU, Fred!!!\n\n' console.log(tm) Dear Fred, This is to inform you, Fred, that you are IN VIOLATION of Penal Code 64.302-4. Surrender yourself IMMEDIATELY! THIS MEANS YOU, Fred!!! undefined > 
0
Dec 12 '17 at 18:33
source share

I wrote a function that solves the problem exactly.

The first argument is the string to be parameterized. You should put your variables on this line, for example, in the format "% s1,% s2, ...% s12" .

Other arguments are parameters respectively for this line.

 /*** * @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]; }); } 

Examples

 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" 

If the variable position changes on this line, this function also supports it without changing the parameters of the function.

 parameterizedString("i have %s2 %s1 and %s4 %s3.", "books", 5, "pencils", "6"); // returns "i have 5 books and 6 pencils." 
0
Jul 09 '19 at 17:41
source share



All Articles