Really struggling to find the right wording for this issue, I am sure the answer is posted somewhere and has to do with not understanding fully how RegExp.prototype.exec() works.
I have a large object with keys that are strings and follow this pattern. "foo.bar.baz": 'test'
where each segment is of arbitrary length, and all the keys start with foo, but the rest vary. I need to construct an array of all of the middle segments, ie. 'bar' in this example. To do this I am using a simple regex grouping.
const myRegex = /foo\.([^\.]*)\./g
String starts with foo, followed by a ".", and then capture everything until the next ".".
Iterating over the object with the keys described above, I want to append the middle section (bar) to an array each time, excluding duplicates.
for(const key in myObject){ //loop over each key in the object
const matches = []
match = myRegex.exec(key)
if (match) {
if (matches.indexOf(match[1]) === -1) { //exclude duplicates
matches.push(match[1])
}
}
The strange behavior is the fact that this only works for all of the second segments that have more than one occurrence. For example given the folowing object.
{
'foo.bar.bar': 'test',
'foo.bar.baz': 'test',
'foo.baz.bar': 'test',
'foo.baz.baz': 'test',
'foo.bam.bar': 'test'
}
Only bar, and baz get pushed to matches. If foo.bam.baz: 'test'
is added to the object, bam is matched and pushed to the array. If one of the foo.bar.x
or foo.baz.x
is removed, then that key is not pushed. It appears to only be matching on the second attempt, and I cannot figure out why.
Any insight is greatly appreciated.