views:

35

answers:

2

I have this string:

{example1}{example2}{example3}

This is the regular expression to find these { anything in it }:

/\{.*?\}/g

Now I want to know how put them in an array so I can do a FOR IN statement.

EDIT

I want an array something like array("{example1}","{example2}","{example3}"); ?

+2  A: 
your_array = string.match( pattern )

http://www.w3schools.com/jsref/jsref_match.asp

hookedonwinter
Will this make an array such like `array("{example1}","{example2}","{example3}");` ?
CIRK
It's javascript, just go try it out in your friendly local browser/firebug...
Xzhsh
@CIRK ya. http://jsfiddle.net/PKpyn/ (with alert of results)
hookedonwinter
he's a beginner i think you won't help him by posting those function singatures ;)
zolex
@zolex true. I like to at least link to w3schools instead of the straight documentation, as they have examples and the "try it now" feature.
hookedonwinter
+1  A: 
var matches = '{example1}{example2}{example3}'.match(/\{.*?\}/g);
// ['{example1}', '{example2}', '{example3}']

See it here.

Also, you should probably use a for loop to iterate through the array. for in can have side effects, such as collecting more things to iterate through the prototype chain. You can use hasOwnProperty(), but a for loop is just that much easier.

For performance, you can also cache the length property prior to including it in the for condition.

alex
nice, this is en example he can learn from :)
zolex
@zolex Thanks, care for an upvote? :P
alex
hm it says "You last voted on this answer 9 hours ago Your vote is now locked in unless this answer is edited"
zolex
@zolex weird, it says no votes whatsoever for this answer
alex