-3

I am very new to regex and JavaScript.

I need a regex for validate only alphanumeric characters and full stop (.), comma(,), colon(:), and semicolon(;).

keyser
  • 18,829
  • 16
  • 59
  • 101
cosmichy
  • 15
  • 1
  • 1
  • 4
  • 2
    This really isn't a question. You should first learn the language basics, including some regular expression basics, give it a try and ask a question if you get stuck somewhere specific. – I Hate Lazy Oct 10 '12 at 14:56

1 Answers1

5

If you want to verify that the complete line contains only the allowed characters:

var regexp = new RegExp(/^[a-zA-Z0-9.,:;]+$/);

^ matches at the beginning of the line

[] matches one of the surrounded characters

+ makes the previous element one or more times

$ matches the end of the line

If the string is allowed to be empty, then turn the + into a *

lilalinux
  • 2,903
  • 3
  • 43
  • 53
  • Thanks so much, This help me a lot, especially for the use of (*) or (+). I need to validate a String with characters repeated no more that a specific number, for example 2. I think in /^[a-z]{0,2}$/ but i miss something. – cosmichy Oct 10 '12 at 15:32
  • 1
    The {0,2} part is correct. Your regex matches strings of length 0, 1 or 2, consisting only of lower case letters. You might want to show us some example string that you want to match (or exclude). BTW: for thanking you can mark the answer as the accepted ;-) – lilalinux Oct 11 '12 at 06:14