Javascript sadly lacks ability to template on external strings unlike C# and many other languages. I made this script to add this to javascript. If you really want you can convert this into a prototype of string but I would not recommend that due collision risk. If you really want it here is a paste => https://pastecode.io/s/6sspezv0
function | returns |
---|---|
externalStringLiterals | Promise with templateString variable on .then |
let demoString = "Hey ladies drop it down just wanna to see {1} {2}"
externalStringLiterals(demoString, ['drop it down','why they didnt made this a feature bru']).then( str =>
console.log(str)
)
- Escape data goes to regex in case of user input to prevent broken matches
- Use
await
Code is licensed under public domain, documentation is under CC BY SA
Thanks for posting this. I ended up making some improvements to include the $ (so that brackets can be in the string too without tripping it up). The changes also enable both named variables and numeric (position based in string array) as well as a combination of the two. Added finish if there were no replacements found in the string.
I use it like this:
Here is the code: