Last active
September 6, 2021 14:53
-
-
Save tristanls/356ce5aea0054b770d49 to your computer and use it in GitHub Desktop.
ISO-8601 Duration Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var iso8601durationregex = /^P([0-9]+(?:[,\.][0-9]+)?Y)?([0-9]+(?:[,\.][0-9]+)?M)?([0-9]+(?:[,\.][0-9]+)?D)?(?:T([0-9]+(?:[,\.][0-9]+)?H)?([0-9]+(?:[,\.][0-9]+)?M)?([0-9]+(?:[,\.][0-9]+)?S)?)?$/ |
The week designator is missing, so a valid ISO 8601 Duration like "P2W" (=2 Weeks") does not match.
I ended up using /^(-?)P(?=\d|T\d)(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)([DW]))?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/
instead.
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: http://en.wikipedia.org/wiki/ISO_8601#Durations
Parses: