-3

I want to be able to get a string in between a bracket opened ( and a bracket closed ).

myString = "MT Mokwa (40,000)"

How can I get 40,000 from the string?

Yax
  • 2,127
  • 5
  • 27
  • 53
  • Use a regular expression. Or even just `indexOf` and `substring` – Matt Burland Jun 06 '17 at 20:24
  • If the format is always similar you could use a reg ex – Andy Jun 06 '17 at 20:24
  • Possible duplicate of [How do you access the matched groups in a JavaScript regular expression?](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression) – Alejandro C. Jun 06 '17 at 20:24
  • Please show us that you've at least done some research into this, as requested in [ask]; "Search and research". – Heretic Monkey Jun 06 '17 at 20:25

1 Answers1

10

Both of these examples assume that the string will always have a set of parenthesis, opening before closing.

I suggest using substring and indexOf for this:

var result = myString.substring( myString.indexOf( '(' ) + 1, myString.indexOf( ')' ) );

You can also use a regex if you prefer:

var result = /\(([^)]*)\)/.exec(myString)[1];
Paul
  • 139,544
  • 27
  • 275
  • 264