I would like to have a regular expression that checks if a string contains only upper and lowercase letters, numbers, and underscores.
This works for me, found this in the O'Reilly's "Mastering Regular Expressions":
/^\w+$/
Explanation:
Verify yourself:
const regex = /^\w+$/;
const str = `nut_cracker_12`;
let m;
if ((m = regex.exec(str)) !== null) {
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}