Last active
January 1, 2026 16:36
-
-
Save Mistium/a4bfeffc4e735b3b44395cbb540473f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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