首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >得到HH: mm:ss,mm:ss regex

得到HH: mm:ss,mm:ss regex
EN

Stack Overflow用户
提问于 2021-05-27 22:41:19
回答 3查看 159关注 0票数 0

我有一个字符串,我想在那里提取一个时间戳。时间戳可以在字符串中的任何位置,而时间戳可以是不同的长度。想想youtube视频时间戳吧。

下面是一个例子

从你的27:19:45经历k-12中,你会为教育而改变什么?#改变

我想抽出27:19:45。但是,其他字符串只能包含MM:SS。

我能想到的是

代码语言:javascript
复制
const timeRegex = /(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)/gm;

当我运行这个正则表达式时,它也会得到字符串的k-12部分中的12,这是我不想要的。

抱歉我删除了秒部分。如果只是几秒钟,它就会有0:12。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-05-27 22:45:34

使用

代码语言:javascript
复制
(?<!\S)(?:(?:(\d{1,2}):)?([0-5]?\d):)?([0-5]?\d)(?!\S)

正则证明

在您的表达式中,您需要使MM部分成为强制的。

解释

代码语言:javascript
复制
--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    \S                       non-whitespace (all but \n, \r, \t, \f,
                             and " ")
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      (                        group and capture to \1:
--------------------------------------------------------------------------------
        \d{1,2}                  digits (0-9) (between 1 and 2 times
                                 (matching the most amount possible))
--------------------------------------------------------------------------------
      )                        end of \1
--------------------------------------------------------------------------------
      :                        ':'
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
    (                        group and capture to \2:
--------------------------------------------------------------------------------
      [0-5]?                   any character of: '0' to '5' (optional
                               (matching the most amount possible))
--------------------------------------------------------------------------------
      \d                       digits (0-9)
--------------------------------------------------------------------------------
    )                        end of \2
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    [0-5]?                   any character of: '0' to '5' (optional
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    \S                       non-whitespace (all but \n, \r, \t, \f,
                             and " ")
--------------------------------------------------------------------------------
  )                        end of look-ahead
票数 1
EN

Stack Overflow用户

发布于 2021-05-27 22:46:03

这一项应能发挥作用:

代码语言:javascript
复制
const timeRegex = /\d\d:\d\d(:\d\d)?/gm

或者,两个冒号之间的数字可以是一个或两个:

代码语言:javascript
复制
const timeRegex = /\d\d?:\d\d?(:\d\d?)?/gm

测试:

代码语言:javascript
复制
s = "What would you change for education from your 27:09:02 experience k-12? #change'"
-> "What would you change for education from your 27:09:02 experience k-12? #change'"
s.match(/\d\d?:\d\d?(:\d\d?)?/gm)
-> ["27:09:02"]
s = "What would you change for education from your 27:09 experience k-12? #change'"
-> "What would you change for education from your 27:09 experience k-12? #change'"
s.match(/\d\d?:\d\d?(:\d\d?)?/gm)
-> ["27:09"]
s = "What would you change for education from your experience k-12? #change'"
-> "What would you change for education from your experience k-12? #change'"
s.match(/\d\d?:\d\d?(:\d\d?)?/gm)
-> null
票数 1
EN

Stack Overflow用户

发布于 2021-05-27 22:57:11

您可以使用正向和正向后两种方式,只有在时间的两边都有\s时才能选择。

代码语言:javascript
复制
const str =
  "What would you change for education from your 27:19:45 and 12:12 and 11 experience k-12? #change";

const result = str.match(/(?<=\s)(\d\d:?)+(?=\s)/g);
console.log(result);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67730775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档