To go ahead and get a point out there, instead of repeatedly using these:
[A-Za-z0-9 _]
[A-Za-z0-9]
I have two (hopefully better) replacements for those two:
[\w ]
[^\W_]
The first one matches any word character (alphanumeric and _
, as well as Unicode) and the space. The second matches anything that isn't a non-word character or an underscore (alphanumeric only, as well as Unicode).
If you don't want Unicode matching, then stick with the other answers. But these just look easier on the eyes (in my opinion). Taking the "preferred" answer as of this writing and using the shorter regexes gives us:
^[\w ]*[^\W_][\w ]*$
Perhaps more readable, perhaps less. Certainly shorter. Your choice.
EDIT:
Just as a note, I am assuming Perl-style regexes here. Your regex engine may or may not support things like \w and \W.
EDIT 2:
Tested mine with the JS regex tester that someone linked to and some basic examples worked fine. Didn't do anything extensive, just wanted to make sure that \w and \W worked fine in JS.
EDIT 3:
Having tried to test some Unicode with the JS regex tester site, I've discovered the problem: that page uses ISO instead of Unicode. No wonder my Japanese input didn't match. Oh well, that shouldn't be difficult to fix:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Or so. I don't know what should be done as far as JavaScript, but I'm sure it's not hard.