Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how I convert string into interpolated string?

I'm receiving a string from the backend. Let's say

"Save Earth, ${name}"

At the frontend, I'm using javascript I want to know how can I bind name from the variable getting in the string.

I know about the string interpolation in JS like below

`Save Earth, ${name}`

Even on using string interpolation is not helping me and I'm getting normal string from backend like below:

  const name = "John"; 
  const response = "Save Earth, ${name}";
  console.log(`${response}`);

What I am getting

"Save Earth, ${name}"

Expected

"Save Earth, John"

NOTE: I can't use string find & replace method because the variable name can be anything.

EDIT: Do I need to use Regex to find & replace method only here.

Why I am looking for another approach? Because the string can be lengthy and I feel that will increase the time complexity.

like image 224
harish kumar Avatar asked Jul 16 '20 09:07

harish kumar


People also ask

How do you use string interpolation?

As the colon (":") has special meaning in an interpolation expression item, to use a conditional operator in an interpolation expression, enclose that expression in parentheses. string name = "Horace"; int age = 34; Console. WriteLine($"He asked, \"Is your name {name}?\ ", but didn't wait for a reply :-{{"); Console.

What is interpolation string?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

Why do we use string interpolation?

The string interpolation feature is built on top of the composite formatting feature and provides a more readable and convenient syntax to include formatted expression results in a result string.

What is interpolation syntax?

The interpolation syntax is powerful and allows you to reference variables, attributes of resources, call functions, etc. You can also perform simple math in interpolations, allowing you to write expressions such as ${count. index + 1} .


2 Answers

For this you need use replace function with regex With /${\w+}/ variable what have you defined.

const name = "John";
const response = "Save Earth, ${name}";

const newResponse = response.replace(/\${\w+}/ ,name);

console.log(newResponse);
like image 64
Vijay Palaskar Avatar answered Oct 18 '22 23:10

Vijay Palaskar


Find & replace can stil be used with a regex.

You could use an object instead of seperate variables with the data you have what to replace. Then inside the replace function you can pass a function as second parameter which returns the correct data.

The reason for an object is that we can't access variables by name (unless you add them to the window object).

const response = "${name} is ${age} years old!";

const data = {
  name: "John Doe",
  age: "42"
};

const replaceVariable = (match, inner) => {
  return data[inner] || "";
}

const replaced = response.replace(/\$\{(\w+)\}/gi, replaceVariable);

console.log(replaced);
like image 34
Reyno Avatar answered Oct 18 '22 23:10

Reyno