0

Ok, let's say you have a string: "(hello) (world) (something) (number 4)" And in javascript, you wanted to get the contents between the brackets in chronological order. You can use indexOf() but how can you deal with multiple possibility.

Pixeladed
  • 1,773
  • 5
  • 14
  • 26

1 Answers1

1

using match and map may be an idea:

"(hello) (world) (something) (number 4)"
 .match(/\(.+?\)/g)
 .map(function(a){return a.replace(/[\(\)]/g,'');})
//=> ["hello", "world", "something", "number 4"]

See MDN on the Array.prototype.map method, also offers a shim for older browsers

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • did the `map` method replace all `()` with nothing? and how did you put it in an array? – Pixeladed Aug 17 '13 at 12:13
  • The `match` methods' result is an array containing all parenthesized words of the string (as per the regular expression `/\(.+?\)/g`). The `map` method replaces the parentheses of every element of that array with an empty string. – KooiInc Aug 17 '13 at 13:13