I have a line that can be in the following formats (numbers are examples):
2h34m2s 23m6s 7h8s 4h2m 3h 2m 2s
I was originally going to use split() with a regex to match h , m and s . However, this would not work if only some indicators were present (if it is a 2m3s form, then I need a way to find out that the first record in the array represents minutes, not hours).
I assume that the correct solution will be associated with some regex expression, but it eludes me. Any ideas for a clean and efficient solution?
edit:
(I would like to accept any number of digits, so I do not use {1,2} in the regular expression.
var timeString = $('#interval').val(); var hours = timeString.match('/\d+(?=h)/')[0]; // 2h4m8s var minutes = timeString.match('/\d+(?=m)/')[0]; var seconds = timeString.match('/\d+(?=s)/')[0]; console.log('hours: ' + hours); console.log('minutes: ' + minutes); console.log('seconds: ' + seconds);
The console gives null for values.
source share