matchAll() requires match() to be called first

Can anybody explain why my call to matchAll is only working when preceded by a call to match()

const testValues = [
    "Host(`example.local`)",
    "Host(`foo.example.local`) || Host(`bar.example.local`)",
    "HOST(`foo.example.local`) || ( Host(`baz.example.local`) && Path(`/baz`) )",
    "Host(`bill.example.local`) || ( Path(`/ben`) && Host(`ben.example.local`) )"
]

const re = /Host\(`(.*?\.local)`\)/gi

testValues.forEach( l => {
    if (re.test(l)) {
        l.match(re)
        const matches = [...l.matchAll(re)]
        for (const match of matches) {
            console.log(match[1])
        }
    } else {
        console.log("no match - " + l )
    }
})

it I comment out the l.match(re) line then the following l.matchAll(re) doesn't return the required groups.



Read more here: https://stackoverflow.com/questions/65728111/matchall-requires-match-to-be-called-first

Content Attribution

This content was originally published by hardillb at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: