Minutes and seconds in seconds

I have a line captured from a page in “4m 26s” format, how can I split this into a few seconds?

Many thanks,

+3
source share
4 answers
var str = "4m 26s";
var arr = str.split(" ");
var sec = parseInt(arr[0], 10)*60 + parseInt(arr[1], 10);

You do not need regex if you use parseInt ...

+2
source

A 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);
+3
source

Non-modal path:

Do string.split(" ")in your line; then do string.slice(0, -1)on both arrays. Multiply the first entry by 60. Add them together.

+2
source
var str = "4m 26s";
console.log(str.match(/\d+m\s+(\d+)s/)[1]);//26
0
source

Source: https://habr.com/ru/post/1752058/


All Articles