最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Basic RegExp examples in Javascript - Stack Overflow

programmeradmin1浏览0评论

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 allow 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

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 allow 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

Share Improve this question edited Aug 23, 2011 at 11:28 Chakradhar asked Aug 23, 2011 at 8:41 ChakradharChakradhar 7736 gold badges14 silver badges29 bronze badges 1
  • gskinner./RegExr I would try to write some for you but they would catch that I am that weird VIM guy. – James Andino Commented Aug 23, 2011 at 11:24
Add a ment  | 

2 Answers 2

Reset to default 2
  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 ing from (as in is there other content in the line).

Tested using regexpal

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

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

发布评论

评论列表(0)

  1. 暂无评论