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!

TSAF
  • 13
  • 4

2 Answers2

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/

Tushar
  • 85,780
  • 21
  • 159
  • 179
  • if I have multiple values then how can I use substring consider i have string like this `'this is an example of '` and i need to find values between '<' and '>' this – Vignesh Jun 11 '18 at 12:34
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

Sudharsan S
  • 15,336
  • 3
  • 31
  • 49