I'm new to regex and trying to create the expression for a string that is to consist of only:
So far I have this:
/[a-zA-Z0-9,\.\_]/;
As I said I'm fairly new so expect this to be totally wrong!
Thanks.
/^[a-z0-9._]+$/i
EDIT: David Thomas suggests a nice alternative: /^[\w.]+$/i
-- possible locale issues.
You have a comma that I don't think you intend to have, but if you do just put it back. the i
modifier makes the expression case-insensitive.
Inside of character classes, periods don't need to be escaped (not relevant if you do: http://jsfiddle.net/pvgTT/1/)
You need the ^
and $
(beginning and end of string) to match against the entire string. Otherwise, it could match a string that has at least one such character but could also have others.
+
(1 or more) is used. Could also be *
(0 or more). Either is required since the length of the string is unknown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With