2

i need to replace some data obtained from match();

This one return string that contain "Total time: 9 minutes 24 seconds"

data.match(/Total time: [0-9]* minutes [0-9]* seconds/);

but i need only "9 minutes 24 seconds", I try use:

data.match(/Total time: [0-9]* minutes [0-9]* seconds/).replace("Total time:", "");

but there is an error ""

".replace is not a function"

Can some one help me?

2
  • This looks very similar to your earlier question: stackoverflow.com/questions/8119585/parsing-string-with-grep . What is the actual problem you are trying to solve? Commented Nov 15, 2011 at 2:33
  • @Roman, do you need any further help with this problem? Commented Nov 21, 2011 at 9:27

4 Answers 4

4

Use capturing sub expressions in your regex:

var match = data.match(/Total time: ([0-9]* minutes [0-9]* seconds)/);
alert(match[1]);

match() returns an array, which is why you can't call replace on the result — there is no Array#replace method.

Sign up to request clarification or add additional context in comments.

Comments

1
data = 'Total time: 15 minutes 30 seconds';
response = data.match(/Total time: [0-9]* minutes [0-9]* seconds/);
response = response[0];
alert(response.replace("Total time:", ""));

Comments

1

You could get rid of using match doing something like this...

var match = data.replace(/Total time: ([0-9]* minutes [0-9]* seconds)/,"$1");

Comments

0

JavaScript will return an array of matches or null if no match is found. The original code attempts to call the replace method on an instance of an Array instead of the element (a String) within it.

var result = null;
var m = data.match(/.../);
if (m) {
  result = m[0].replace('Total time: ', '');
}

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.