var expression = "[AccNum]==true&&[AccNum]==[ARID]&&[AccNum]==aaaa || [ARID]!=true&&[DOB]>[ARID] || [DOB]>bbb&&[DOS]>=[ARID]&&[DOS]<[Gender]&&[66642]<=cccc&&[66642] In (ddd,fff,ggg) || [FirstName] NotIn (hhh,jjj,kkk)&&[FirstName] StartsWith lll || [Gender] EndsWith mmm";

我需要将此表达式拆分为一个数组,

键为“ &&”和“ ||”但是我也需要数组中的键。

最佳答案

var expression = "[AccNum]==true&&[AccNum]==[ARID]&&[AccNum]==aaaa || [ARID]!=true&&[DOB]>[ARID] || [DOB]>bbb&&[DOS]>=[ARID]&&[DOS]<[Gender]&&[66642]<=cccc&&[66642] In (ddd,fff,ggg) || [FirstName] NotIn (hhh,jjj,kkk)&&[FirstName] StartsWith lll || [Gender] EndsWith mmm";

var converted = expression
                .split(/(&&|\|\|)/)
                .map(item => item.trim());

console.log(converted)





用括号括起来的RegEx分隔符将把分隔符和各项一起推送到数组中,这要感谢:https://stackoverflow.com/a/12002085/3499595

映射以修剪每个项目,删除空格。

10-06 05:21