capture group regex javascript code example
Example 1: javascript access matched group in regex
string = "some string containing var userId = 117051";
var foundId = string.match(/var userId = (\d+)/)[1];
console.log(foundId); // "117051"
Example 2: javascript regex named capture group
const auth = 'Bearer AUTHORIZATION_TOKEN'
const { groups: { token } } = /Bearer (?<token>[^ $]*)/.exec(auth)
console.log(token) // Prints "AUTHORIZATION_TOKEN"