Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format string template with variables in Javascript

I want to use a statically defined template for URL building.

I'm trying to use ES6 string interpolation feature for this

var template = "http://example.com/?name=${name}&age=${age}";  var name = "John"; var age = "30";  var url = `${template}`; 

Expected result: http://example.com/?name=John&age=23

Actual result: http://example.com/?name=${name}&age=${age}

In case this can't be done with string interpolation is there any better method than String.prototype.replace like

var url = template.replace(/\${name}/,"John").replace(/\${age}/, 23); 
like image 224
humkins Avatar asked Feb 16 '17 16:02

humkins


People also ask

What is `` in JS?

Backticks ( ` ) are used to define template literals. Template literals are a new feature in ECMAScript 6 to make working with strings easier. Features: we can interpolate any kind of expression in the template literals. They can be multi-line.

Which of the following is the correct format for template string in JavaScript?

In JavaScript, the template string implements the string interpolation. A template string is defined by wrapping a sequence of characters into a pair of backticks `I'm template string` . The template string placeholders have the format ${expression} , for example `The number is ${number}` .

How do you interpolate a string in JavaScript?

You can add values into a JavaScript string using a template literal. This is a dollar sign followed by a pair of curly brackets. Within the curly brackets should be the expression whose value you want to embed in the string.


2 Answers

The variables are substituted at the moment of evaluation of the literal, so you can't create a universal template that can be substituted with variables later:

var template = `http://example.com/?name=${name}&age=${age}`; var name = "John"; var age = "30";  console.log( template ); // "http://example.com/?name=undefined&age=undefined" 

Edit: fiddle for those who reuse a console session and have the variables defined from previous experiments: https://jsfiddle.net/nwvcrryt/

You also can not convert a string literal "My name is ${name}" to a template like what you're trying to do.

You can, however, use a function that takes the name and age and returns the desired url:

const formatUrl = (name, age) => `http://example.com/?name=${name}&age=${age}`; let name = "John"; let age = "30"; let url = formatUrl( name, age ); // "http://example.com/?name=John&age=30" 
like image 182
pawel Avatar answered Sep 19 '22 21:09

pawel


Here is how you would handle this issue if the values came after and you still wanted to use the template:

var template = (name, age) => `http://example.com/?name=${name}&age=${age}`;  // these come after template is defined var name = "John"; var age = "30";  console.log(template(name, age)); 

This is if the question was in regards to recursion:

You used double quotes " instead of a backtick `

It will work otherwise:

var name = "John"; var age = "30"; var template = `http://example.com/?name=${name}&age=${age}`  var url = `${template}`; 

https://jsfiddle.net/kab48ht5/


If all you're trying to do is get some values into a proper URL format, you can try and follow this solution: https://stackoverflow.com/a/22678412/185672

like image 20
Phil Avatar answered Sep 19 '22 21:09

Phil