Created
September 21, 2020 19:16
-
-
Save TravisL12/237c2d6bebec0be441add56fa7071aff to your computer and use it in GitHub Desktop.
Loop through text to group variables and values
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 groupThese(text) { | |
| let idx = 0; | |
| let count = 0; | |
| const groups = {} | |
| const re = new RegExp(/(?<=const |var |let )([^\s=]+)\s?=\s?["']?(#\w*)["']?/,"i"); | |
| while (count < 10 && idx >= 0) { | |
| const match = text.slice(idx).match(re) | |
| if (match?.index) { | |
| groups[match[1]] = match[2] | |
| idx = match.index + idx; | |
| } else { | |
| idx = -1; | |
| } | |
| count++; | |
| } | |
| return groups; | |
| } | |
| groupThese(`const val = "#ffffff"; | |
| lkjkljk const mehere = "#456333"; | |
| var another = "#123fff"; | |
| lkjlkjlk const notGood = 'hello its me'; | |
| somehitng let final = "#xyzfff";`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment