Ada*_*dam 15 javascript regex arrays
我有这个字符串:
{example1}{example2}{example3}
Run Code Online (Sandbox Code Playgroud)
这是找到这些的正则表达式{
anything in it
}
:
/\{.*?\}/g
Run Code Online (Sandbox Code Playgroud)
现在我想知道如何将它们放入数组中,以便我可以做一个for in
声明.
我想要一个类似的数组array("{example1}","{example2}","{example3}");
?
hoo*_*ter 17
your_array = string.match( pattern )
Run Code Online (Sandbox Code Playgroud)
http://www.w3schools.com/jsref/jsref_match.asp
ale*_*lex 13
var matches = '{example1}{example2}{example3}'.match(/\{.*?\}/g);
// ['{example1}', '{example2}', '{example3}']
Run Code Online (Sandbox Code Playgroud)
此外,您应该使用for
循环来遍历数组.for in
可以有副作用,例如收集更多东西来迭代原型链.你可以使用hasOwnProperty()
,但for
循环更容易.
为了提高性能,您还可以在将length
属性包含在for
条件中之前对其进行缓存.