在一个有趣的项目上工作,我在编写正则表达式方面遇到了一些麻烦。
使用正则表达式和Javascript,我想将输入字符串拆分为“键”数组。具有无效键的字符串应返回0键(null,空数组,无论什么都可以)。
<input string> => <matches>
# named keys
a => ['a']
b => ['b']
foo[bar] => ['foo', 'bar']
# numeric keys are ok too
a[0] => ['a', '0']
b[1] => ['b', '1']
# 'push' syntax is also valid
push[] => ['push', '']
hello[] => ['hello', '']
# complex!
complex[named][0][] => ['complex', 'named', '0', '']
# named keys must start with [a-zA-Z]
_invalid => null
3foo[abc] => null
# named keys can include a number as long as they start with a letter
foo3valid[this_is_valid_2] => ['foo3valid', 'this_is_valid_2']
var regex = /^(?:\[?([a-zA-Z][a-zA-Z0-9_]*|[a-zA-Z0-9_]*)\]?)+$/g;
var keys = [];
myInput.replace(regex, function(match,capture,pos,source){
keys.push(capture);
});
console.log(myInput, keys);
我的正则表达式仅匹配最后一个“键”,例如
# should be ['a', 'b', 'c']
a[b][c] => ['c']
答案 0 :(得分:1)
会是这样的吗?
var input = "a[b][c]";
input = input.replace(/[\[\]]$/,"").replace(/[\]\[]+/g,"|");
var args = input.split("|");
这是一个演示: http://jsfiddle.net/vZBtj/
答案 1 :(得分:1)
我采取了另一种方法来获得理想的结果:
代码:小提琴:http://jsfiddle.net/MVJZc/2/
///Example
var myInput = "a[b][][c]";
var validate = /^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,
pattern = /[a-zA-Z0-9_]+|(?=\[\])/g,
//(?=\[\]) is used to create an empty entry for []
keys = null;
if (validate.test(myInput)) {
keys = myInput.match(pattern);
}
注意:如果要扩展模式,例如要包含$
字符,请修改第一个RegEx。第二个RegEx(pattern
)应该至少包含所有有效字符。
答案 2 :(得分:0)
这是另一种可能性,绝对不像Rob W's那样简洁,但完成了工作; - )
function parseThing(aStr){
var res = [];
success = false;
if(aStr.indexOf('[') > 0){
var idx = aStr.indexOf('[');
var first = aStr.substring(0, idx);
if(first.match(/\w/)){
success = true;
res.push(first);
//now parse stuff inside brackets
var rest = aStr.substring(idx, aStr.length-1);
var arr = rest.split("]");
for(i in arr){
res.push(arr[i].replace("[", ""));
}
}
} else if(aStr.match(/\w/)){
res.push(aStr);
}
return res;
}
console.log(parseThing("a[a][b]"));
console.log(parseThing("a"));
console.log(parseThing("b"));
console.log(parseThing("foo[bar] "));
console.log(parseThing("a[0]"));
console.log(parseThing("b[1]"));
console.log(parseThing("complex[named][0][]"));