Question 4
Question
Answer
/(?<name>\w+)\s+(?<age>\d+)/const regex = /(?<name>\w+)\s+(?<age>\d+)/; const match = regex.exec("John 30"); if (match) { console.log(match.groups.name); // Output: "John" console.log(match.groups.age); // Output: "30" }
const emailRegex = /(?<username>\w+)\@(?<domain>\w+\.\w+)/;
const email = "john.doe@example.com";
const match = emailRegex.exec(email);
if (match) {
console.log("Username:", match.groups.username); // Output: Username: john.doe
console.log("Domain:", match.groups.domain); // Output: Domain: example.com
}Last updated