Skip to content

Instantly share code, notes, and snippets.

@jserrao
Created August 20, 2020 15:25
Show Gist options
  • Save jserrao/95bff85e44a89538055515ba14e8c4a0 to your computer and use it in GitHub Desktop.
Save jserrao/95bff85e44a89538055515ba14e8c4a0 to your computer and use it in GitHub Desktop.
Multi-dimensional array regex hell
// 3- Take listItemsArray and loop through it w/ regex
for (var i = 0; i < listItemsArray.length; i++) {
// 4- Use regex to break each list item into five pieces
listItemsTemp[i] = listItemsArray[i].match(/(<li\b(?:>\s+(?:id="([^"]*)"|class="([^"]*)")|[^\s>]+|\s+)*>)|((<\w+\b>)|(<a\shref=".*">)|(.+?(?=<))|(<\/\w+\b)>)/g);
// 5- Setup listItemsRearranged[i] with an array of listItemsTemp.length
listItemsRearranged[i] = new Array(listItemsTemp.length);
// 6- Take listItemsTemp and reassign positions
// We can do this because our string will always have five pieces
listItemsRearranged[i][0] = listItemsTemp[i][1];
listItemsRearranged[i][1] = listItemsTemp[i][0];
listItemsRearranged[i][2] = listItemsTemp[i][2];
listItemsRearranged[i][3] = listItemsTemp[i][4];
listItemsRearranged[i][4] = listItemsTemp[i][3];
// 7- Join the array back into a string
listItemsRearrangedConcat[i] = listItemsRearranged[i].join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment