Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - Letters, numbers and symbols

Tags:

jquery

regex

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.

like image 277
A B Avatar asked Dec 07 '22 11:12

A B


1 Answers

/^[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.

like image 122
Explosion Pills Avatar answered Dec 11 '22 11:12

Explosion Pills