nelsonm

msg:4522638 | 6:34 pm on Nov 26, 2012 (gmt 0) |
Never mind... i got it! It should look like: "/^(1[012]|[1-9])\s*[aA|pP][mM]\s*(-\s*(1[012]|[1-9])\s*[aA|pP][mM])?$/i" this will allow zero or more spaces between the... 1. starting hour and am/pm 2. starting am/am and "-" 3. "-" and the ending hour 4. ending hour and am/pm and also allow lower or upper case am/am. Question... Is it possible to add some regex to allow but still collapse zero or multiple spaces between items 1 tru 4 into one space? thanks.
|
g1smd

msg:4522658 | 8:37 pm on Nov 26, 2012 (gmt 0) |
[aA] allows both cases, but so does the /i flag. [a|p] is "a" or "pipe" or "p". You need [ap] here. You don't need a group for [m]. It's a literal. Simplifies to
"/^(1[012]?|[2-9])\s?[ap]m\s?(-\s?(1[012]?|[2-9])\s?[ap]m)?$/i" Having found the "1" once, there's no need to backtrack to find it again when the 1 isn't followed by 0, 1 or 2.
|
nelsonm

msg:4522671 | 9:05 pm on Nov 26, 2012 (gmt 0) |
g1smd, Yes your modification is simpler - thanks. However, I'd like to offer a little error free flexibility in allowing the user to mistakenly enter zero or "more" spaces between items 1 tru 4 in my post as apposed to only allowing zero or "one" space. So I'll keep the "*" after the "\s" instead of "?". Thanks.
|
g1smd

msg:4522674 | 9:27 pm on Nov 26, 2012 (gmt 0) |
Yes, if you want multiple spaces, or none, then \s* is fine.
|
|