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.
- 2Check this: Javascript regex - how to get text between curly bracketsZbigniew– Zbigniew2013-08-17 11:10:48 +00:00Commented Aug 17, 2013 at 11:10
- 1What do you mean with "chronological" order?Caspar Kleijne– Caspar Kleijne2013-08-17 11:11:32 +00:00Commented Aug 17, 2013 at 11:11
- from num 1 to the lastPixeladed– Pixeladed2013-08-17 12:04:59 +00:00Commented Aug 17, 2013 at 12:04
Add a comment |
1 Answer
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
2 Comments
Pixeladed
did the
map method replace all () with nothing? and how did you put it in an array?KooiInc
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.