var a = "11 seconds 2 minutes 5 hours 2 days 9 weeks 4 months 10 years"; // for debugging
window.match = function(s) {
var s = s.toLowerCase(),
time = {},
i18n= {
second: 1,
minute: 60,
hour: 3600,
day: 86400,
week: 604800,
month: 2592000,
year: 31536000
},
m = s.match(/\d+ (second|minute|hour|day|week|month|year)/g);
if ($.isArray(m)) {
// found match(es)
for (var i in m) {
var a = m[i].split(" ");
time[a[1]] = Number(a[0]) * i18n[a[1]];
}
var timeCount = 0
for (var i in time) {
timeCount += time[i];
}
if (timeCount == 0) {
console.log("Invalid time! equal to 0 seconds");
} else {
console.log(timeCount);
}
} else {
// no matches found - use default time
console.log("Invalid time! (no match(es) found)");
}
}
match(a);
Advertisement
94
pages
MatchTime
Advertisement