Javascript regular expression to validate time format "09:15 AM" - javascript

I tried with the following regular expression but it is not working as expected.
/^([01]?[0-9]|2[0-3]):[0-5][0-9]{2}(AM|PM)/

You can try using this regex:
^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$
REGEX DEMO

You can try like this
/^[1-12]:[0-59](A|P)M$/i

You are missing space before AM|PM and there is unnecessary {2}
The following changes works for me:
^([01]?[0-9]|2[0-3]):[0-5][0-9] (AM|PM)
On the string
09:15 AM

Related

Regular expression for numbers beginning with 0300, 0500, 0800, 0808

Can any one tell me regular expression that no. should start with:
0300
0500
0800
0808
I am using this but it is not working
(0800|0300)\d
You can use this:
0[358]0[08]\d*
If you are looking for only one such number on each line in a file, you can use this:
^0[358]0[08]\d*$
0[358][08]\d*
should work. What doesn't work for you?

How to validate 12-Hour Time with regex (regular Expression)

I want to validate a 12-Hours time format like (01:05 PM) in PHP, Javascript and HTML.
I tried this:
((([1-9])|(1[0-2])):([0-5])(0|5)\s(A|P)M)
But I don't know why it doesn't work.
Try ,
^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)$
You may test here
try this :
(((0[1-9])|(1[0-2])):([0-5])([0-9])\s(A|P)M)
test regex here
try this for case sensitive AM|PM|am|pm
: `
(((0[1-9])|(1[0-2])):([0-5])(0|5)\s(A|P|a|p)(M|m))
`
This will work for both pm/am and PM/AM and prefixed with or without 0(Zero)
/^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/

Javascript regex for dates

Could somebody write a JavaScript compatible RegExp to match the below formats of dates?
ex.
11.09.
11.9
11.9.
11.9.2014
11.9.14
I tried
var regex=new RegExp("([0-2]{1}[0-9]{1}|3[0-1]{1})[.](0[1-9]|1[0-2])[.][0-9]{4})");
but don't know how to update it to capture everything listed above. It only works for 11.09.2014.
You can use this regex:
^(0?[1-9]|[12]\d|3[0-1])[.](0?[1-9]|1[0-2])(?:[.](?:\d{2}|\d{4})?)?$
RegEx Demo

How to modify time regexp?

I am newbie in Regular Expressions.
I need to check this time 18:00(00:00, 01:00, 05:12, any time of this format) by regexp, is it correct or no.
Here is my variant:
/[0-9][0-9]:[0-9][0-9]/
Can I change [0-9][0-9] to something like 2*[0-9] ???
If you specifically want to match 18:00 you should use /^\d{2}:\d{2}$/. The answers provided so far will match aaa12:99ddd. The ^ and $ are anchors and will instruct the regex to match specifically the given string.
If you are after a 24 hour validation regular expression you could use this: /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/ (taken from here).
You can also write it like this: /^\d{2}:\d{2}$/
You can modify it by adding a repetition clause:
/^[0-9]{2}:[0-9]{2}/
But you should add the starting and ending clauses :
/^[0-9]{2}:[0-9]{2}$/

Javascript RegExpression

I am looking for a regular expression that will accept date formats, both MM/DD/YYYY and M/D/YYYY
I found one here
However when I run that I get an unexpected illegal token. Here is how I am implementing the javascript function
return RegExp(/^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/).test(txtDate);
I've been told the http://regexlib.com website usually works well for .net regular expressions, but not javascript.
Any help would be great!
Escape the slashes inside the regex.
/^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])\/(29|30)|(0?[13578]|1[02])\/31)\/(19|[2-9]\d)\d{2}|0?2\/29\/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/
here __^ __^ __^ ___^ ___^___^
for MM/DD/YY /^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/
for M/D/Y /^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/
Recipe 4.4 from Regex Cookbook does most of what you are looking for:
^(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2}$
Take a look at this:
http://jsfiddle.net/oscarj24/32JQr/
hope this helps

Categories