Reputation: 1912
I need extract only "40:37.298s" from:
Total time: 40:37.298s
using JS, but I`m new in JS, can some one help?
Upvotes: 1
Views: 1996
Reputation: 15220
I would say use a RegEx like [:\.0-9]*s
for example
yourText.match("regex")
in your case it will be
"Total time: 40:37.298s".match("[:\.0-9]*s")[0]
It will return 40:37.298s
Upvotes: 0
Reputation: 60424
There are many ways to do this. Here's one:
var str = "Total time: 40:37.298s";
str.split(": ")[1]
In most cases I prefer splitting on some known pivot, rather than trying to extract a specific substring (as others have shown) for the following reasons:
Upvotes: 2
Reputation: 35407
var time = 'Total time: 40:37.298s';
time = time.match(/\d{1,2}:\d{1,2}\.\d+s/);
Upvotes: 1
Reputation: 5213
var item = 'Total time: 40:37.298s';
var pattern = /\d{1,2}\:\d{2}\.\d{3}s/g;
var res = pattern.exec(item);
That is:
Upvotes: 0
Reputation: 2992
var result = "Total time: 40:37.298s".replace(/^.*([0-9]{2}:[0-9]{2}\.[0-9]{3}s)/g,'$1')
Upvotes: 0
Reputation: 1758
the substring method is what you're looking for:
"Total time: 40:37.298s".substring("Total time: ".length);
Upvotes: 0
Reputation: 318598
> 'Total time: 40:37.298s'.substr(12)
'40:37.298s'
If you want to use a regex for more flexibility:
> /([0-9]+:[0-9]+\.[0-9]+s)/.exec('Total time: 40:37.298s')[1]
'40:37.298s'
Upvotes: 6