1

I have string: "You can vote 36 times for topics and 84 times for comments".

I need to get number 84 from string (number can be different).

How can I do it?

7
  • Doesn't need to try if knows nothing about regex Commented Jun 26, 2012 at 12:20
  • 2
    Ah there is good number of what-have-you-tried-Guys here, noobs and beginners, no luck for you guys. Say thanks to @qwertymk who is NOT what-have-you-tried-Guy like me :) Commented Jun 26, 2012 at 12:22
  • 1
    @Blaster: Asking does not hurt anyone. Even an answer such as "I don't have any idea how to approach this" might give some idea about the level of knowledge. Commented Jun 26, 2012 at 12:27
  • 1
    There are many related questions btw. Commented Jun 26, 2012 at 12:29
  • 1
    @Blaster: When I commented, qwertymk already provided is answer, so that's more likely to be the reason that one else answered. It's a reminder to include any related research effort. Granted, this question did not necessarily need it, but I rather comment one time too often than not enough. Commented Jun 26, 2012 at 12:35

2 Answers 2

8

Try this:

/^You can vote \d\d times for topics and (\d\d) times for comments$/.exec(str);

Or:

/^You can vote \d+ times for topics and (\d+) times for comments$/.exec(str);
Sign up to request clarification or add additional context in comments.

1 Comment

@tucnak then click the arrow next to the answer.
2

OR try this lil different: demo http://jsfiddle.net/GyK7J/

code

var s = "You can vote 36 times for topics and 84 times for comments".;
var firstnumber = parseInt(/vote\s(\d+)/.exec(s)[1], 10);
var second = /and\s([\d+]+)/.exec(s)[1];

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.