Skip to content

Instantly share code, notes, and snippets.

@utkuozdemir
Last active January 18, 2023 16:42
Show Gist options
  • Save utkuozdemir/aff7a347f9759a07bcf231da3df591a0 to your computer and use it in GitHub Desktop.
Save utkuozdemir/aff7a347f9759a07bcf231da3df591a0 to your computer and use it in GitHub Desktop.
JS regex to validate Go time.Duration
^[-+]?(((\d+(\.\d*)?|\d*(\.\d+)+)(ns|us|µs|ms|s|m|h))|0)+$
@utkuozdemir
Copy link
Author

utkuozdemir commented Jan 18, 2023

Matches all parseDurationTests and parseDurationErrorTests in Go's standard test suite except the overflowing ones:

https://regex101.com/r/xNTblv/2

Unicode flag (/u) must be used with the pattern for the µ character to be matched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment