我正在使用JavaScript正则表达式解析FortiGate日志,并且在返回重复值时遇到了一些麻烦。这是我的字符串:
Jan 30 12:14:26 [23.4.56.4] date=2013-01-30,time=12:14:26,devname=FGT60C3G665236574,device_id=FGT60C3G665236574,log_id=0021000002,type=traffic,subtype=allowed,pri=notice,vd=root,src=10.10.89.107,src_port=59216,src_int=""internal"",dst=15.26.25.195,dst_port=53640,dst_int=""wan1"",SN=556987,status=accept,policyid=1,dir_disp=org,tran_disp=snat,proto=6,duration=120,sent=240,rcvd=238,sent_pkt=4,rcvd_pkt=3
我使用以下正则表达式来获取devname
/devname=(.*?),/
这是我运行以下代码时返回的内容
var reDeviceName = /devname=(.*?),/;
var log3='Jan 30 12:14:26 [23.4.56.4] date=2013-01-30,time=12:14:26,devname=FGT60C3G665236574,device_id=FGT60C3G665236574,log_id=0021000002,type=traffic,subtype=allowed,pri=notice,vd=root,src=10.10.89.107,src_port=59216,src_int=""internal"",dst=15.26.25.195,dst_port=53640,dst_int=""wan1"",SN=556987,status=accept,policyid=1,dir_disp=org,tran_disp=snat,proto=6,duration=120,sent=240,rcvd=238,sent_pkt=4,rcvd_pkt=3';
console.log(log3.match(new RegExp(reDeviceName)));
这是返回的内容:
[0] => devname=FGT60C3G665236574
[1] => devname=
[2] => FGT60C3G665236574
我怎样才能返回介于两者之间的东西 DEVNAME = FGT60C3G665236574,
并返回一个信息实例FGT60C3G665236574
答案 0 :(得分:1)
您可以使用:
console.log(log3.match(new RegExp(reDeviceName))[1]);
请注意,实际数组没有您在问题中提到的第一个元素。
请注意,您可以直接使用:
var reDeviceName = /devname=(.*?),/;
var log3='Jan 30 12:14:26 [23.4.56.4] date=2013-01-30,time=12:14:26,devname=FGT60C3G665236574,device_id=FGT60C3G665236574,log_id=0021000002,type=traffic,subtype=allowed,pri=notice,vd=root,src=10.10.89.107,src_port=59216,src_int=""internal"",dst=15.26.25.195,dst_port=53640,dst_int=""wan1"",SN=556987,status=accept,policyid=1,dir_disp=org,tran_disp=snat,proto=6,duration=120,sent=240,rcvd=238,sent_pkt=4,rcvd_pkt=3';
console.log(log3.match(redeviceName)[1]);
[省略new RegExp()
构造函数]