I’m new to regex and trying to create the expression for a string that is to consist of only:
- letters
- numbers
- Underscore symbol
- Period symbol (full stop)
So far I have this:
/[a-zA-Z0-9,\.\_]/;
As I said I’m fairly new so expect this to be totally wrong!
Thanks.
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
imodifier 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.