Use a capturing group:
const regex = /{([$0-9a-zA-Z_]+)(?=.*})/g
const variable = []
let m;
while (m = regex.exec(segment)) {
variable.push(m[1])
}
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
{ '{'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[$0-9a-zA-Z_]+ any character of: '$', '0' to '9', 'a'
to 'z', 'A' to 'Z', '_' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
} '}'
--------------------------------------------------------------------------------
) end of look-ahead