OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

how to use javascript to convert template literal that was stored as string back to template literal that output the variable in a function?

  • Thread starter Thread starter ey dee ey em
  • Start date Start date
E

ey dee ey em

Guest
Code:
const a = "abc";
const tlAsText = "I dont like1 ${a}";
const converted = String.raw`${tlAsText}`;
console.log(converted); // it should output "I dont like1 abc" instead of "I dont like1 ${a}"

Code Sanbox To experiment:

https://codesandbox.io/p/sandbox/js-rh338g?file=/src/index.mjs

Tried many answers on google and chat gpt. to no avail. i.e. eval, String.raw, etc.

The end goal is that I can store such template literal as string somewhere in the database and reuse it later anywhere I want as long as those variables are there. Unless there is an alternative to do this.

<pre><code>const a = "abc";
const tlAsText = "I dont like1 ${a}";
const converted = String.raw`${tlAsText}`;
console.log(converted); // it should output "I dont like1 abc" instead of "I dont like1 ${a}"
</code></pre>
<p>Code Sanbox To experiment:</p>
<p><a href="https://codesandbox.io/p/sandbox/js-rh338g?file=/src/index.mjs" rel="nofollow noreferrer">https://codesandbox.io/p/sandbox/js-rh338g?file=/src/index.mjs</a></p>
<p>Tried many answers on google and chat gpt. to no avail. i.e. eval, String.raw, etc.</p>
<p>The end goal is that I can store such template literal as string somewhere in the database and reuse it later anywhere I want as long as those variables are there. Unless there is an alternative to do this.</p>
 

Latest posts

Top