I'm trying to check for a valid time value it can be anything between 00 hours 00 Mins and 00 secs and 23 hours 59min and 59 secs.
I want it to check that it matches exactly HH:MM:SS with 0 being used if there no information for part not sure if I am explain this well so an example would be.
If the time was 23 mins and 5 secs it would be
00:23:05 and reject 23:05
The expression I think is right is
^(?:(?:([01]\d|2[0-3]):)([0-5]\d):)([0-5]\d)$
I am using jQuery but as far as I know there no easy way to validate time.
Am I correct?
That looks okay to me, but if you're just validating, it would be easier not to use grouping constructs when you don't need to, which would leave something like:
^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$
That only uses grouping for the alternation operator, which makes it much easier to read, IMO.
Related
I am using this below regex to check 01 to 12 and hardcoded 21 to 32 to validate. Looking for better solution without hardcoding.
^(?=(?:[0][0-9]|1[0-2])|21|22|23|24|25|26|27|28|29|30|31|32)\d{9}$
Thanks for your help!
See if as simple as below caters to your need:
/^(0[1-9]|1[0-2]|2[1-9]|3[0-2])$/
If leading 0 is optional, you can make it to:
/^(0?[1-9]|1[0-2]|2[1-9]|3[0-2])$/
Haven't tested it completely, but I'm sure you'll get an idea.
With some addresses a building might take up multiple door numbers for example 13 - 15 StreetName.
The "13 - 15" is the part I am focusing on. How would you do a regular expression to pick out this part.
I thought something like [0-9] - [0-9] which works for 1 - 3 but if the address was 12 - 13 [0-9][0-9] - [0-9][0-9] could work but then I want to make sure that something like 13 - 3 wouldnt work as the addresses cannot go backwards and something like 99 - 103 would also work where the numbers are different lengths. Is it really simple and I'm missing something?
I'm still a student and not very good at regular expressions, I just need it for some js I'm doing and have spent far too long getting nowhere.
Thank you.
There's not really a good way to do this since you're effectively trying to parse something that is not a regular language. Referencing something that you've seen before is allowed by several regular expression languages though, but that won't help you in this specific case.
We can easily go for the brute-force solution though :)
https://regex101.com/r/4bRmiL/1
^(\d{3} - \d{3,}|\d{2} - \d{2,}|\d - \d+)$
As you can see though, it still breaks for cases like 5 - 1 which are probably invalid. That's something you need to check outside of the regex.
I don't think this is even possible with regex. I would instead just do something like this:
var addresses = [
"12 - 14 State St.",
"14 - 12 State St.",
];
addresses.forEach(address => console.log(validAddress(address)));
function validAddress(address) {
return !!(address.match(/\d+\s?-\s?\d+/) || []).filter(a => {
var numbers = a.split('-').map(b => b.trim());
return (numbers.length && numbers[0] < numbers[1]);
}).length;
}
Cant seem to figure why this works
moment("30\\Nov\\2016 22:14","DD\\MM\\YYYY HH:mm").toString()
(The result is "Wed Nov 30 2016 22:14:00 GMT+0000")
and this does not work
moment("31\\Oct\\2016 22:14","DD\\MM\\YYYY HH:mm").toString()
(The result is "Invalid date").
Does anybody have any idea why this is happening?
edit
changed the date above
edit 2
tried this snippet
moment("30\\Jan\\2016 22:14","DD\\MMM\\YYYY HH:mm").toString()
result = "Wed Nov 30 2016 22:14:00 GMT+0000"
thats strange
A couple of issues here:
Firstly your date format using backslashes is causing a problem (single or double backslashes both cause different issues) in certain instances. I'm not sure exactly what the issue is, since the first example works, but I suspect it's treating it as some sort of escape sequence.
Secondly, "MM" is the wrong token to use to parse short month names. It should be "MMM". It seems coincidence that it works for your "Nov" string when using "MM", but it certainly doesn't work for "Oct" or most others.
If you can change your data source to provide dates using a different separator (/ or - are pretty standard) then do that. If not, you might have to do a string replace on the date string just before you feed it to momentJS.
Examples of strings that don't work (either produce incorrect dates, or report "Invalid Date"):
"31\\Oct\\2016 22:14","DD\\MMM\\YYYY HH:mm"
"31\Oct\2016 22:14","DD\MMM\YYYY HH:mm"
"31/Oct/2016 22:14","DD/MM/YYYY HH:mm"
As you can see, it's evolved almost to the point of a parseable string, which would look like:
"31/Oct/2016 22:14","DD/MMM/YYYY HH:mm"
I'm trying to build a time tracking application which needs to parse a string which can have (but not always) a time in it in one of many formats or combinations. The formats I'm checking for are
h|hr|hrs|hours
m|min|mins|minutes
These can be with a space in-between the number and the hour or minutes and can be combined or just one or the other. Some examples:
1h
1 hour 20 mins
2hrs 15 m
The regex for matching the times that I have currently:
((\d+(\.\d+)?)\s*(h|hr|hrs|hours))?(\s*(\d+)\s*(m|min|mins|minutes))?
This works fine if I just pass it the time string without anything before it. My problem is that I want to parse a full text string with the time appearing anywhere in it. Some examples:
The is a time entry for some work 1h 15m
2h 45mins This is a time entry for some work
This is a time entry 1hour 25 mins for some work
This is a time entry for some work
Does anyone have any suggestions on how to tackle this?
All you really need to do with yours is make the plural s optional, and add some word boundary tokens.
Try:
\b((\d+(\.\d+)?)\s*(h|hr|hrs?|hours?))?(\s*(\d+)\s*(m|min|mins?|minutes?))?\b
So I was looking at how I could display a Desktop Notification using a Google Chrome extensions when I came across these lines of code:
var time = /(..)(:..)/(Date()); // The prettyprinted time.
var hour = time[1] % 12 || 12; // The prettyprinted hour.
var period = time[1] < 12 ? 'a.m.' : 'p.m.'; // The period of the day.
What the heck does all of this do?
Fascinating, I've not seen this before:
/regex/(...);
EDIT: see this!
This:
/(..)(:..)/(Date());
// seems to emulate the functionality of exec()
Will return the match (array of matched groups) of the regular expression, /(..)(:..)/, against the string (Date()):
"Thu Jul 08 2010 09:40:38 GMT+0200 (W. Europe Daylight Time)"
(or whatever time it happens to be)
The returned array (the match), in this case, is:
["09:40", "09", ":40"]
This line:
var hour = time[1] % 12 || 12;
...simply determines the hour. If the hour is falsey (i.e. 0) then it defaults to 12 -- this makes it possible for the next statement to return the correct am/pm suffix. (12:00 is am).
The first line is using a regular expression to extract the time element from the string returned by Date(). For instance, this might be '08:37' The brackets in this regular expression give two different 'groups' of characters, the first group matching '08', the second matching '37'
The second line is taking the first set of characters, which will be automatically converted to a number, and getting the remainder of division by 12. Presumably to turn a 24 hour clock number into a 12 hour clock number. '|| 12' acts to return 12 just in case the remainder is 0.
The third line uses a ternary conditional operator to add 'a.m' just in case the hour is smaller than 12, otherwise 'p.m.'