Regular Expressions 101

Community Patterns

Time duration from minutes to weeks suffixed by letter and whitespace separated

1

Regular Expression
ECMAScript (JavaScript)

/
^(?<weeks>\d{1,2})w\s(?<days>0?[0-6])d\s(?<hours>(?:0?|1|2(?=[0-3]))\d)h\s(?<minutes>(?:0?|[1-5])\d)m$
/
i

Description

Brief Description

Time duration from minutes to weeks suffixed by corresponding letter and whitespace separated. No unit of time can be omitted. Max values are enforced for minutes, hours, and days, and a max length for weeks. Case is ignored. The string should match completely from start to end.

Exemple: 3w 2d 10h 15m

Specs

Time units suffixes

  • w for weeks
  • d for days
  • h for hours
  • m for minutes

Max values

  • 99 for weeks
  • 6 for days
  • 23 for hours
  • 59 for minutes

Min values: 0 or 00 for all

Separators: any single whitespace character \s

Case is ignored

Full match RegEx

Capture Groups The following capture groups capture the number segments of the respective time unit.

  • weeks
  • days
  • hours
  • minutes
Submitted by Dorin Popa - 3 years ago (Last modified 3 years ago)