get variables inside a string within curly braces and replace it with dynamic data in javascript

One way to achieve this is by using template literals. Template literals are a type of string that allow for dynamic expressions to be embedded within them using ${}.

Here is an example:

index.tsx
const name = 'John';
const age = 30;
const job = 'developer';

const introduction = `Hi, my name is ${name}. I am ${age} years old and work as a ${job}.`;
155 chars
6 lines

In the example above, the variables name, age and job are embedded within the string using ${}. When the code is executed, the values of these variables will be substituted in place of the expressions.

So, if you were to run the following code:

index.tsx
console.log(introduction);
27 chars
2 lines

You would see the following output:

index.tsx
Hi, my name is John. I am 30 years old and work as a developer.
64 chars
2 lines

gistlibby LogSnag