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.
I want an array something like array("{example1}","{example2}","{example3}");
?
your_array = string.match( pattern )
http://www.w3schools.com/jsref/jsref_match.asp
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With