Created
November 25, 2020 22:31
-
-
Save onliniak/6f2394da77879e324422f8f7064d040d to your computer and use it in GitHub Desktop.
Repeated Regex
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
const str = | |
`A: | |
a: 1 | |
c: 3 | |
b: 9 | |
f: 3 | |
B: | |
a: 3 | |
c: 5 | |
s: 4` | |
const regexp = /(\w):\s*((\w):\s+(\w))*(\w)*/g | |
const array = [...str.matchAll(regexp)] | |
const district = /^\S$|^\S*\S$/gm | |
const str2 = str.replaceAll(/:/g, '') | |
const array2 = [...str2.matchAll(district)] | |
let i,g | |
function dynamicArrayVariables(variable) { | |
window[variable] = [] | |
} | |
array2.forEach(dynamicArrayVariables) | |
for (i = 0; i < array.length; i++) { | |
const array = [...str.matchAll(regexp)] | |
if (typeof array[i][5] === 'undefined') | |
g = i | |
if (typeof array[i][3] != 'undefined'){ | |
let key = array[i][3] | |
window[array[g][1]].push({ [key]: array[i][4] }) | |
} | |
if (typeof array[i][3] === 'undefined') | |
if (array[i][1] != array[i][3]){ | |
let key = array[i][1] | |
window[array[g][1]].push({ [key]: array[i][5] }) | |
} | |
} | |
console.log(A) # => Array [Object { a: "1" }, Object { c: "3" }, Object { b: "9" }, Object { f: "3" }] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Magic repeated regex in Firefox 84