Skip to content

Instantly share code, notes, and snippets.

@Mistium
Last active January 1, 2026 16:36
Show Gist options
  • Select an option

  • Save Mistium/a4bfeffc4e735b3b44395cbb540473f4 to your computer and use it in GitHub Desktop.

Select an option

Save Mistium/a4bfeffc4e735b3b44395cbb540473f4 to your computer and use it in GitHub Desktop.
function parseTemplate(str) {
let depth = 0;
let cur = '';
const arr = [];
for (let i = 0; i < str.length; i ++) {
if (str[i] + str[i + 1] === '${') {
if (depth === 0) {
arr.push(cur);
cur = "$";
}
depth ++;
continue;
}
if (str[i] === '}') {
depth --
if (depth === 0) {
arr.push(cur + '}');
cur = "";
}
continue;
};
cur += str[i];
}
arr.push(cur);
return arr;
}
console.log(parseTemplate("hello ${lol + `${battery}%`} crazy"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment