我有一个字符串,我想在那里提取一个时间戳。时间戳可以在字符串中的任何位置,而时间戳可以是不同的长度。想想youtube视频时间戳吧。
下面是一个例子
从你的27:19:45经历k-12中,你会为教育而改变什么?#改变
我想抽出27:19:45。但是,其他字符串只能包含MM:SS。
我能想到的是
const timeRegex = /(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)/gm;
当我运行这个正则表达式时,它也会得到字符串的k-12部分中的12,这是我不想要的。
抱歉我删除了秒部分。如果只是几秒钟,它就会有0:12。
发布于 2021-05-27 22:45:34
使用
(?<!\S)(?:(?:(\d{1,2}):)?([0-5]?\d):)?([0-5]?\d)(?!\S)见正则证明。
在您的表达式中,您需要使MM部分成为强制的。
解释
--------------------------------------------------------------------------------
(?<! 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发布于 2021-05-27 22:46:03
这一项应能发挥作用:
const timeRegex = /\d\d:\d\d(:\d\d)?/gm或者,两个冒号之间的数字可以是一个或两个:
const timeRegex = /\d\d?:\d\d?(:\d\d?)?/gm测试:
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发布于 2021-05-27 22:57:11
您可以使用正向和正向后两种方式,只有在时间的两边都有\s时才能选择。
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);
https://stackoverflow.com/questions/67730775
复制相似问题