I would like to have a regular expression that checks if a string contains only upper and lowercase letters, numbers, and underscores.
How about:
^([A-Za-z]|[0-9]|_)+$
...if you want to be explicit, or:
^\w+$
...if you prefer concise (Perl syntax).