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.