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"]
精彩评论