JavaScript 正则表达式将时间字符串拆分为单独的字符串数组
JavaScript RegEx to split a string of time to an array of separate strings
所以,我有这个字符串 5h2m40s67ms
。
我如何拆分它以便得到 5h
、2m
、40s
和 67ms
?
这是为了稍后我可以使用 node_module ms
将它们转换为时间戳,并将它们添加到以毫秒为单位的总时间中。
我已经尝试过 /[0-9][a-zA-Z]/g
,但它给出 0s
而不是 40s
和 7m
而不是 67ms
。
此过程是否有替代方法可以将连接的时间字符串(如 5h2m40s67ms
)转换为时间戳?
据我所知,split()
会消耗分隔符。 match()
在这里可能是更好的选择:
const input="5h2m40s67ms";
const parts=input.match(/\d+[hms]+/g);
for(let part of parts)
console.log(part);
哦,你需要 +
来匹配一个或多个同类字符。
所以,我有这个字符串 5h2m40s67ms
。
我如何拆分它以便得到 5h
、2m
、40s
和 67ms
?
这是为了稍后我可以使用 node_module ms
将它们转换为时间戳,并将它们添加到以毫秒为单位的总时间中。
我已经尝试过 /[0-9][a-zA-Z]/g
,但它给出 0s
而不是 40s
和 7m
而不是 67ms
。
此过程是否有替代方法可以将连接的时间字符串(如 5h2m40s67ms
)转换为时间戳?
split()
会消耗分隔符。 match()
在这里可能是更好的选择:
const input="5h2m40s67ms";
const parts=input.match(/\d+[hms]+/g);
for(let part of parts)
console.log(part);
哦,你需要 +
来匹配一个或多个同类字符。