我知道,如果将正则表达式传递给String的match方法,它就会起作用。但是问题是如何编写这样的正则表达式。
所涉及字符串的描述:
1.骨架:字符串的构成如下:
'UNION(xxx,xxx,xxx,xxx,...)'
2. xxx:“ xxx”部分类似于“ DESCENDANTS(“ U0EXMDAW”,MEMBERS(“ U0EXMDAW”),SELF)'或'DESCENDANTS(“ U0EXMDAW”,FILTER(DESCENDANTS(“ U0EXMDAW”,MEMBERS(“ U0EXMDAW” ),ALL),“ name” =“ adf”),ALL)'。DESCENDANTS字符串可能像它本身一样包含多个DESCENDANTS字符串。 DESCENDANTS字符串的结构类似于'DESCENDANTS(“ U0EXMDAW”,MEMBERS(“ U0EXMDAW”),SELF)'。现在的问题是如何从联合字符串中剥离'xxx'部分?输入:'UNION(DESCENDANTS(“ U0EXMDAW”,MEMBERS(“ U0EXMDAW”),SELF),DESCENDANTS(“ U0EXMDAW”,FILTER(DESCENDANTS(“ U0EXMDAW“,MEMBERS(” U0EXMDAW“),ALL),” name“ =” adf“),ALL)))输出:['DESCENDANTS(” U0EXMDAW“,MEMBERS(” U0EXMDAW“),SELF)','DESCENDANTS( “ U0EXMDAW”,FILTER(DESCENDANTS(“ U0EXMDAW”,MEMBERS(“ U0EXMDAW”),ALL),“ name” =“ adf”),ALL)']]
最佳答案
也许一些正则表达式会更好,但我认为小型解析器就足够了。
string = 'UNION(DESCENDANTS("U0EXMDAW", MEMBERS("U0EXMDAW"), SELF),DESCENDANTS("U0EXMDAW", FILTER(DESCENDANTS("U0EXMDAW", MEMBERS("U0EXMDAW"), ALL), "name" = "adf"), ALL))';
s = string.substring(6, string.length -1); //we strip out the 'union' and its parentheses
//Let's parse the children
children = [];
depth = 0;//number of parentheses we pass
while( s.length) {
var i =0;
for(i; i < s.length; i++) {
if(s[i] == "(") {
depth++;
}
if(s[i] == ")") {
depth--;
}
if(s[i] == "," && depth == 0) {
break;
}
}
children.push(s.substring(0, i));
s = s.substring(i+1);
}
console.log(children); // ['DESCENDANTS("U0EXMDAW", MEMBERS("U0EXMDAW"), SELF)', 'DESCENDANTS("U0EXMDAW", FILTER(DESCENDANTS("U0EXMDAW", MEMBERS("U0EXMDAW"), ALL), "name" = "adf"), ALL)']
参见小提琴https://jsfiddle.net/me1k8tbt/1/
关于javascript - 如何剥离字符串的某些部分并将主题放入数组中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35122047/