1

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?

7 Answers 7

6
> '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'
Sign up to request clarification or add additional context in comments.

Comments

2

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:

  • It's more flexible; this will still work if the first part of the string contains slight variations (but the substring method won't)
  • It's easier to see what's being extracted (but the substring method requires that I manually count positions in the string to verify that I'm selecting the right thing)

Comments

1
var time = 'Total time: 40:37.298s';

time = time.match(/\d{1,2}:\d{1,2}\.\d+s/);

http://jsfiddle.net/ypzgJ/

Comments

0

the substring method is what you're looking for:

"Total time: 40:37.298s".substring("Total time: ".length);

Comments

0
var result = "Total time: 40:37.298s".replace(/^.*([0-9]{2}:[0-9]{2}\.[0-9]{3}s)/g,'$1')

Comments

0
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:

  • \d{1,2} one or two digits
  • \: the ':' char
  • \d{2} two digits
  • . the '.' char
  • \d{3} three digits
  • s the 's' char

1 Comment

Note, that Regexp .exec() will assign matches in an Array to the res, so to get the actual matched value from the code above, you'll need to query res[0].
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.