Reputation: 13
I have a string that I am trying to retrieve a value from between two certain characters. I know there are multiple questions like this on here, but I couldn't find one that searches for multiple instances of this scenario in the same string.
Essentially I have a string like this:
'(value one is: 100), (value two is:200)'
and I want to return both 100 and 200. I know that I can write a regex to retrieve content between two characters, but what is the best way to have a function iterate over the string for the :
character and grab everything from that until the )
character and only stop when there are no more instances?
Thanks in advance!
Upvotes: 0
Views: 1608
Reputation: 87233
For your case, you can use regex
to get the numbers from string.
var str = '(value one is: 100), (value two is:200)';
var regex = /\d+/g;
str.match(regex);
Here \d+
will match the numbers from string. g
is global flag to match all the elements and not the only first.
Demo: http://jsfiddle.net/tusharj/k96y3evL/
Upvotes: 2
Reputation: 15403
Using Regex
var regex = /\d+/g;
var string = '(value one is: 100), (value two is:200)';
var match = string.match(regex);
alert(match);
Upvotes: 0