开发者

Convert element into array using PrototypeJS

Given I have a string - invoice[level1][level2][level3] and I am using Prototype as Javasc开发者_开发问答ript framework.

How can I turn that string into an array like this ['level1', 'level2', 'level3']


Strip everything till the starting [. Remove the ending ] and then split on ][

str.substring(str.indexOf('[') + 1, str.length - 1).split('][')


Something like this?

theString.match(/\[(.*?)\]/g)

Example (tested in Chrome):

var str = 'invoice[level1][level2][level3]';
results = new Array();
str.match(/\[(.*?)\]/g).each(
  function(item){ 
    results[results.length] = item.substring(1, item.length - 1);
  }
)
-> results contains -> ["level1", "level2", "level3"]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜