javascript通过关键字和don';如果用括号括起来,则不能拆分
我有以下输入字符串:javascript通过关键字和don';如果用括号括起来,则不能拆分,javascript,regex,string,Javascript,Regex,String,我有以下输入字符串:关键字=>和/或 let query = `(Application = http AND "Server Port" "Not In" 80,443,8080) AND "Client Port" Between 22,33 OR ("File Size" In 220,400,500 AND "File Size" "Not In" 5,10,60) OR "Client IP" !=
关键字=>和/或
let query = `(Application = http AND "Server Port" "Not In" 80,443,8080)
AND "Client Port" Between 22,33
OR ("File Size" In 220,400,500 AND "File Size" "Not In" 5,10,60)
OR "Client IP" != 1.0.2.1
OR Application = skype`;
//I want split the above string into following format
[
'(Application = http AND "Server Port" "Not In" 80,443,8080)',
'AND',
'"Client Port" Between 22,33',
'OR'
'("File Size" In 220,400,500 AND "File Size" "Not In" 5,10,60)'
'OR'
'"Client IP" != 1.0.2.1'
'OR',
'Application = skype'
]
我有下面的代码,或者用关键字分割字符串,或者如果字符串完全用括号括起来
query.split(/AND|OR/g); //Split based on key words
************
var str = '((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)';
var final = str.replace(/\((?!\()/g,"['") //replace ( with [' if it's not preceded with (
.replace(/\(/g,"[") //replace ( with [
.replace(/\)/g,"']") //replace ) with ']
.replace(/\sAND\s/g,"','AND','") //replace AND with ','AND','
.replace(/\sOR\s/g,"','OR','") //replace OR with ','OR','
.replace(/'\[/g,"[") //replace '[ with [
.replace(/\]'/g,"]") //replace ]' with ]
.replace(/"/g,"\\\"") //escape double quotes
.replace(/'/g,"\""); //replace ' with "
console.log(JSON.parse("["+final+"]"))
忽略括号拆分字符串有什么帮助吗?基本上,我想提取任何内容(+关键字),包括括号。您可以使用以下正则表达式:
query.split(/AND|OR/g); //Split based on key words
************
var str = '((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)';
var final = str.replace(/\((?!\()/g,"['") //replace ( with [' if it's not preceded with (
.replace(/\(/g,"[") //replace ( with [
.replace(/\)/g,"']") //replace ) with ']
.replace(/\sAND\s/g,"','AND','") //replace AND with ','AND','
.replace(/\sOR\s/g,"','OR','") //replace OR with ','OR','
.replace(/'\[/g,"[") //replace '[ with [
.replace(/\]'/g,"]") //replace ]' with ]
.replace(/"/g,"\\\"") //escape double quotes
.replace(/'/g,"\""); //replace ' with "
console.log(JSON.parse("["+final+"]"))
query.split(/(AND|OR)(?![^(]*\))/);
这将只匹配一个和
或一个或
,如果后面没有)
且前面没有(
)的话。这是通过“负面展望”完成的:(?!)
通过对空格(\s*
)进行一些修剪,您可以得到以下结果:
var str='((应用程序=smtp和“服务器端口”!=25)和(应用程序=smtp和“服务器端口”!=25))或(应用程序=pop3和“服务器端口”!=110)或(应用程序=imap和“服务器端口”!=143)和(应用程序=imap或“服务器端口”!=143);
var result=str.split(/\s*(和|或)\s*(?![^(]*\)/);
console.log(结果);
.as console wrapper{max height:100%!important;top:0;}
如果存在多个嵌套层,则会导致额外的括号和混乱的拆分:(“服务器端口”==1和(应用程序=smtp和(“服务器端口”!=25和“服务器端口”!=22)))
@Gerrit0,正确。我为这种嵌套的情况添加了一个替代方案,它不能在一个正则表达式中处理(至少不能在JS中处理)。不要使用正则表达式来解析上下文无关的语言!