我在网上找到了一些代码(stackoverflow https://stackoverflow.com/a/5774234/150062),这正是我所需要的。但我似乎无法让它运行。我收到一个错误"'/(\\d+)\\s*(second|min|minute|hour)/g' is not a function (evaluating 'regex(s)')";
var timespanMillis = (function() {
var tMillis = {
second: 1000,
min: 60 * 1000,
minute: 60 * 1000,
hour: 60 * 60 * 1000 // etc.
};
return function(s) {
var regex = /(\d+)\s*(second|min|minute|hour)/g, ms=0, m, x;
while (m = regex(s)) {
x = Number(m[1]) * (tMillis[m[2]]||0);
ms += x;
}
return x ? ms : NaN;
};
})();
我也没听说过这个regex()
功能?它应该是别的东西吗?