正则表达式 – SCORM 2004时间格式 – 正则表达式?

前端之家收集整理的这篇文章主要介绍了正则表达式 – SCORM 2004时间格式 – 正则表达式?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为LMS构建SCORM 2004 javascript API,SCORM 2004的一个要求是传入它的时间间隔必须遵循以下格式.有谁知道这个正则表达式是什么?我试图围绕它,但无济于事.注意:P必须始终是第一个字符.

P[yY][mM][dD][T[hH][nM][s[.s]S]]
where:

  • y: The number of years (integer,>= 0,not restricted)
  • m: The number of months (integer,>=0,not restricted)
  • d: The number of days (integer,not restricted)
  • h: The number of hours (integer,not restricted)
  • n: The number of minutes (integer,not restricted)
  • s: The number of seconds or fraction of seconds (real or integer,not
    restricted). If fractions of a second are used,SCORM further restricts the string to
    a maximum of 2 digits (e.g.,34.45 – valid,34.45454545 – not valid).
  • The character literals designators P,Y,M,D,T,H,M and S shall appear if the
    corresponding non-zero value is present.
  • Zero-padding of the values shall be supported. Zero-padding does not change the
    integer value of the number being represented by a set of characters. For
    example,PT05H is equivalent to PT5H and PT000005H.

Example –

  • P1Y3M2DT3H indicates a period of time of 1 year,3 months,2 days and 3
    hours
  • PT3H5M indicates a period of time of 3 hours and 5 minutes

任何帮助将不胜感激.

谢谢!

更新:

添加了一些必须保留的额外标准 –

  • The designator P shall be present
  • If the value of years,months,days,hours,minutes or seconds is
    zero,the value and corresponding
    character literal designation may be
    omitted,but at least one character
    literal designator and value shall be
    present in addition to the designator
    P
  • The designator T shall be omitted if all of the time components (hours,
    minutes and seconds) are not used. A
    zero value may be used with any of the
    time components (e.g.,PT0S)

解决方法

这是我使用的正则表达式;

^P(?=\w*\d)(?:\d+Y|Y)?(?:\d+M|M)?(?:\d+D|D)?(?:T(?:\d+H|H)?(?:\d+M|M)?(?:\d+(?:\­.\d{1,2})?S|S)?)?$

猜你在找的正则表达式相关文章