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.
Asked
Active
Viewed 65 times
0

Pixeladed
- 1,773
- 5
- 14
- 26
-
2Check this: [Javascript regex - how to get text between curly brackets](http://stackoverflow.com/questions/3354224/javascript-regex-how-to-get-text-between-curly-brackets) – Zbigniew Aug 17 '13 at 11:10
-
1What do you mean with "chronological" order? – Caspar Kleijne Aug 17 '13 at 11:11
-
from num 1 to the last – Pixeladed Aug 17 '13 at 12:04
1 Answers
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