1

如何在 jquery 中拆分带有“AND”和/或“OR”参数的查询?假设我有

A=( 1=1,FirstName='aaa' or FirstName='bbb' and FirstName='ccc' or FirstName='ddd' and FirstName='eee')

现在我想用 'and' & 'or' 拆分 'A' 并得到如下结果

A=(FirstName='aaa' , FirstName='dddd' , FirstName='bbb' , FirstName='dddd' , FirstName='cccc')

但问题是我在查询中可能有也可能没有“或”,但它仍然应该在拆分后产生带有所有值的 ao/p。请帮忙。不同类型的查询可能如下:

A=( 1=1,FirstName='aaa' or FirstName='bbb' and FirstName='ccc' or FirstName='eee' and FirstName='fff')  
A=( 1=1,FirstName='aaa' and FirstName='bbb' and FirstName='ccc' and FirstName='ddd' and FirstName='fff')
A=( 1=1,FirstName='aaa' or FirstName='bbb' or FirstName='ccc' or FirstName='ddd' or FirstName='fff')

查询不是静态的,我只是举了一个例子,它可以是任何字符串。

4

1 回答 1

3

一个鲜为人知的事实是,您可以将正则表达式作为分隔符传递给String.split()方法

var A = "FirstName='aaa' or FirstName='bbb' and FirstName='ccc' or FirstName='ddd' and FirstName='eee'";
A.split(/and|or/g);

/* result:
["FirstName='aaa' ", " FirstName='bbb' ", " FirstName='ccc' ", " FirstName='ddd' ", " FirstName='eee'"]
*/
于 2013-01-17T12:35:55.393 回答