2

I am trying to make sure that the format people input is exactly this :

.match(/\d{1,2}:\d\d\s((AM)|(PM))/)

Meaning that a user could write :

12:30 AM
2:30 PM

But not :

1:2 A
1:30
PM

It needs to be first two digits, followed by a colon, than two more digits, a space, and either AM or PM. But my regex expression isn't that. What am I missing?

4
  • What's the problem? The regex seems to work fine for me. What's it matching that it shouldn't? What's it not matching that it should?
    – gen_Eric
    Mar 19, 2012 at 20:14
  • 2
    That regular expression is perfectly valid and does the thing you want to. So I guess the problem should be somewhere in your code. Also you don't have to use so many brackets - this works the same: \d{1,2}:\d\d\s(AM|PM)
    – user219882
    Mar 19, 2012 at 20:15
  • Your regex actually is that, except that \s means "any whitespace character" rather than simply "a space", and except that you probably want your regex to start with ^ and end with $ so that you know you've matched the entire input string. If you're seeing behavior that makes you think your regex does something different, then the problem is probably with the surrounding code. (Perhaps you're misunderstanding what the match method does?)
    – ruakh
    Mar 19, 2012 at 20:16
  • Can you post the javascript code that is not returning a true match? Your regex seems to be fine.
    – Kash
    Mar 19, 2012 at 20:17

7 Answers 7

6

What exactly seems to be the problem?

> "1:2 A".match(/\d{1,2}:\d\d\s((AM)|(PM))/);
null

>"12:30 AM".match(/\d{1,2}:\d\d\s((AM)|(PM))/);
["12:30 AM", "AM", "AM", undefined]

However:

  1. You need to ground your expression to the start (^) and end ($) of the string otherwise;

    > "foo 12:30 AM foo".match(/\d{1,2}:\d\d\s((AM)|(PM))/);
    ["12:30 AM", "AM", "AM", undefined]
    
  2. Look at RegExp.test() instead, which returns a simpler true/false rather than an array.

    > /^\d{1,2}:\d\d\s((AM)|(PM))$/.test("12:30 AM");
    true
    

A simpler expression which does the same thing could be /^\d{1,2}:\d{2} [AP]M$/

1
  • this regex will also return true for "99:30 PM"
    – c0deNinja
    Mar 19, 2012 at 20:47
4

Assuming that you are checking it on single line input field (and not searching it inside a text area), you should do:

/^\d{1,2}:\d\d\s[AP]M$/
1
  • Very concise expression that does the same thing as intended. Mar 19, 2012 at 20:20
2

How about something like this:

.match(/([0]?[1-9]|1[0-2])(:)[0-5][0-9]?( )?(AM|PM)/)
1

If your problem is new line character. You can try:

'12:30 
AM'.replace(/\n/, '').match(/\d{1,2}:\d\d\s((AM)|(PM))/)
1

Combining the ideas of Matt, c0deNinja and my own you should end up with:

/^(0?[1-9]|1[0-2]):[0-5][0-9]\s?[AP]M$/.test(input);
1

I've tried your code in http://www.regular-expressions.info/javascriptexample.html and it worked.

By the way, you also need to test if the time is valid. Your code now can accept things like this 99:12 AM as if they were correct. I suggest you to use something like this.

\b(1[0-2]|\d):[0-5][0-9]\s([aApP][mM])\b

=)

1
  • 1
    The i flag at the end will make it case insensitive: /\b(1[0-2]|\b\d):[0-5][0-9]\s([AP]M)\b/i Mar 19, 2012 at 20:24
1

Your regex seems to be right. Incorporating some ideas above, you could test your string like this with Regex:

^(1[0-2]|\d):[0-5]\d [aApP][mM]$

And the testing code in Javascript:

var regex = /^(1[0-2]|\d):[0-5]\d [aApP][mM]$/g; 
    var input = "2:30 PM"; 
    if(regex.test(input)) {
      var matches = input.match(regex);
      for(var match in matches) {
        alert(matches[match]);
      } 
    } else {
      alert("No matches found!");
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.