I have a string being grabbed from a page in the format "4m 26s", how can I strip this into just seconds?
Many thanks,
I have a string being grabbed from a page in the format "4m 26s", how can I strip this into just seconds?
Many thanks,
Simple regex will work:
var s = '21m 06s';
var m = /(\d{1,2})m\s(\d{1,2})s/.exec(s);
var mins = parseInt(m[1], 10);
var secs = parseInt(m[2], 10);
var str = "4m 26s";
console.log(str.match(/\d+m\s+(\d+)s/)[1]);//26
A non-regex way:
Do a string.split(" ")
on your string; then do string.slice(0, -1)
on both arrays. Multiply the first entry by 60. Add them together.
var str = "4m 26s";
var arr = str.split(" ");
var sec = parseInt(arr[0], 10)*60 + parseInt(arr[1], 10);
You don't need regex if you use parseInt...