开发者

Basic RegExp examples in Javascript

Can any one give the perfect regular expression for

 1. US date format (mm/dd/yyyy).
 2. Alpha Numerics.
 3. Alpha Numerics with spaces.
 4. age betwenn 15-100.

in java script. i have this Regex.

      /^[0-9a-zA-Z]+$/ (alphanumeric that doesnot allo开发者_开发知识库w spaces)
      /^[0-9]+$/       ( for age but not getting below 100)
      /^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d+$/;   (US date format)

Thanks


  1. I'm guessing you dont want the brackets (0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-9]|3[01])/[0-9]{4}

Note this doesn't validate that the number of days is valid in that month

  1. [a-zA-Z0-9]+?
  2. [a-zA-Z0-9\s]+?
  3. I wouldn't use regex for this but anyway (1[5-9]|[2-9][0-9]|100)

for these you may need to add the bol an eol chars depending on where your input is coming from (as in is there other content in the line).

Tested using regexpal


I think http://regexpal.com/ is a very good resource to learn regexp in general. Combine this with the mozilla docs: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

For 4, you are better of parsing the string as a number with parseInt or parseFloat and then comparing them with an if.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜