0

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!

1

2 Answers 2

2

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/

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

1 Comment

if I have multiple values then how can I use substring consider i have string like this 'this is an example of <how><i have it>' and i need to find values between '<' and '>' this
0

Using Regex

var regex = /\d+/g;
var string = '(value one is: 100), (value two is:200)';
var match = string.match(regex);
alert(match);

Fiddle

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.