我想写一个正则表达式,用逗号分隔字符串,它不在()。
之内示例:
"test,test,test".split(/.../) => var a = ["test", "test", "test"];
"test(123,345),test".split(/.../) => var a = ["test(123,345)", "test"];
"test(123,345),a(b,c)".split(/.../) => var a = ["test(123,345)", "a(b,c)"];
"test(cb(a,b),345),a(b(d,e,f),c),abc".split(/.../) => var a = ["test(cb(a,b),345)", "a(b(d,e,f),c)", "abc"];
我有以下正则表达式,但只有在第一个匹配逗号后没有()时它才有效:
"test,test,test".split(/,(?!.*\))/) => OK
"test(cb(a,b),345),test,test".split(/,(?!.*\))/) => OK
"test,test(cb(a,b),345),test".split(/,(?!.*\))/) => FAIL
答案 0 :(得分:2)
正则表达式不适合这种任务。我认为在你遵循括号嵌套级别的情况下滚动自己的解析器会更容易,以确定是否应该拆分:
function splitTokens(var input) {
var tokens = [];
var currentToken = "";
var nestingLevel = 0;
for (var i = 0; i < input.length; i++) {
var currentChar = input[i];
if (currentChar === "," && nestingLevel === 0) {
tokens.push(currentToken);
currentToken="";
} else {
currentToken+=currentChar;
if (currentChar === "(") { nestingLevel++; }
else if (currentChar === ")") { nestingLevel--; }
}
}
if (currentToken.length) {
tokens.push(currentToken);
}
return tokens;
}
请注意,我没有处理不匹配的括号,您可能希望为这些情况添加逻辑。