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.

3

1 Answer 1

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

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

2 Comments

did the map method replace all () with nothing? and how did you put it in an array?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.