Skip to content

Instantly share code, notes, and snippets.

@TravisL12
Created September 21, 2020 19:16
Show Gist options
  • Select an option

  • Save TravisL12/237c2d6bebec0be441add56fa7071aff to your computer and use it in GitHub Desktop.

Select an option

Save TravisL12/237c2d6bebec0be441add56fa7071aff to your computer and use it in GitHub Desktop.
Loop through text to group variables and values
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