我有动态字符串。
[
"09 11 2015 06:34:09 32.83.112.225 <LOC0:INFO> Sep 11 06:36:48 macyfw005.fds.com ASM:unit_hostname='macyfw005.fds.com',management_ip_address='32.83.112.225',http_class_name='/Common/GLOBAL-PROTECTION',web_application_name='/Common/GLOBAL-PROTECTION',policy_name='/Common/GLOBAL-PROTECTION',policy_apply_date='2015-08-24 06:28:02',violations='',support_id='2285783706446637451',request_status='passed',response_code='204',ip_client='75.114.197.33',route_domain='0',method='GET',protocol='HTTP',query_string='pList=939954_Pos1|375431_Pos2|399520_Pos3|106695_Pos4|375430_Pos5&r=MCOM-NAVAPP&rType=PixelPresented&vId=0abe4d00abff200abcb800abb4200ab54700ab9d600abdba00aba190&cId=9154800489&c=PDP_ZONE_B&cgId=7L0011&dId=439211aa-4f53-4894-a20b-0479f31b44b4&hId=H5',x_forwarded_for_header_value='75.114.197.33, 23.212.53.178',sig_ids='',sig_names='',date_time='2015-09-11 06:36:47',severity='Informational',attack_type='',geo_location='US',ip_address_intelligence='N/A',username='N/A',session_id='600a4641d5b97f83',src_port='39507',dest_port='80',dest_ip='63.73.131.56',sub_violations='',virus_name='N/A',uri='/Event",
"09 11 2015 06:34:14 32.83.112.225 <LOC0:INFO> Sep 11 06:36:53 macyfw005.fds.com ASM:unit_hostname='macyfw005.fds.com',management_ip_address='32.83.112.225',http_class_name='/Common/GLOBAL-PROTECTION',web_application_name='/Common/GLOBAL-PROTECTION',policy_name='/Common/GLOBAL-PROTECTION',policy_apply_date='2015-08-24 06:28:02',violations='',support_id='2285783706502672559',request_status='passed',response_code='200',ip_client='71.191.129.248',route_domain='0',method='GET',protocol='HTTP',query_string='_fields=name,address,attributes,inventories&productId=2223071',x_forwarded_for_header_value='71.191.129.248, 23.66.230.156',sig_ids='',sig_names='',date_time='2015-09-11 06:36:52',severity='Informational',attack_type='',geo_location='US',ip_address_intelligence='N/A',username='N/A',session_id='553f3abd96644e82',src_port='48270',dest_port='80',dest_ip='63.73.131.56',sub_violations='',virus_name='N/A',uri='/api/store/v2/stores/2455',request='GET /api/store/v2/stores/2455?_fields=name,address,attributes,inventories&productId=2223071 HTTP/1.1\r\nReferer: http://www1.macys.com/shop/product/tahari-a",
"09 11 2015 06:35:43 32.83.112.225 <LOC0:INFO> Sep 11 06:38:21 macyfw005.fds.com ASM:unit_hostname='macyfw005.fds.com',management_ip_address='32.83.112.225',http_class_name='/Common/GLOBAL-PROTECTION',web_application_name='/Common/GLOBAL-PROTECTION',policy_name='/Common/GLOBAL-PROTECTION',policy_apply_date='2015-08-24 06:28:02',violations='',support_id='2285783706541561702',request_status='passed',response_code='302',ip_client='216.52.244.107',route_domain='0',method='GET',protocol='HTTPS',query_string='',x_forwarded_for_header_value='127.0.0.1, 96.6.47.31, 216.52.244.107, 184.87.194.181',sig_ids='',sig_names='',date_time='2015-09-11 06:38:21',severity='Informational',attack_type='',geo_location='US',ip_address_intelligence='N/A',username='N/A',session_id='eebdacc806cb7e4',src_port='52897',dest_port='443',dest_ip='63.73.131.56',sub_violations='',virus_name='N/A',uri='/akamai/akamai-sureroute-test-object.htm',request='GET /akamai/akamai-sureroute-test-object.htm HTTP/1.1\r\nX-Akamai-TestObject: true\r\nPragma: no-cache\r\nUser-Agent: Mashery Proxy\r\nX-Mashery-Message-ID: dc71f4fc-5853-4cb7-9",
];
我希望得到
试过
var
match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function(s) {
return decodeURIComponent(s.replace(pl, " "));
};
function getparams(varString) {
var query = varString;
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
return urlParams;
}
和
function getparams(varString) {
var regex = new RegExp("[\\?&] =([^&#]*)"),
results = regex.exec(varString);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
我需要输出如:
{
"management_ip_address":'32.83.112.225',
"User - Agent": "Mashery Proxy\r\nX-Mashery-Message-ID: dc71f4fc-5853-4cb7-9",
"request":"'GET /shop/product/moncler-boys-maya-jacket-sizes-8-14?ID=1068901&CategoryID=23723&LinkType= HTTP/1.1"
}
答案 0 :(得分:0)
这应该适合你:
myArrayOfStrings.map(function(s){
return s.split(',').reduce(function(a, b){
a[b.split('=')[0]] = b.split('=')[1];
return a;
}, {});
}); // [{destIp:11.11.11.11, ..}, {destIp:22.22.22.22, ..}]
我们使用reduce来映射所有字符串(替换它们),其中'='之前的文本和'='之后的文本用于','中的每个字符串。
var array = [
"09 11 2015 06:34:09 32.83.112.225 <LOC0:INFO> Sep 11 06:36:48 macyfw005.fds.com ASM:unit_hostname='macyfw005.fds.com',management_ip_address='32.83.112.225',http_class_name='/Common/GLOBAL-PROTECTION',web_application_name='/Common/GLOBAL-PROTECTION',policy_name='/Common/GLOBAL-PROTECTION',policy_apply_date='2015-08-24 06:28:02',violations='',support_id='2285783706446637451',request_status='passed',response_code='204',ip_client='75.114.197.33',route_domain='0',method='GET',protocol='HTTP',query_string='pList=939954_Pos1|375431_Pos2|399520_Pos3|106695_Pos4|375430_Pos5&r=MCOM-NAVAPP&rType=PixelPresented&vId=0abe4d00abff200abcb800abb4200ab54700ab9d600abdba00aba190&cId=9154800489&c=PDP_ZONE_B&cgId=7L0011&dId=439211aa-4f53-4894-a20b-0479f31b44b4&hId=H5',x_forwarded_for_header_value='75.114.197.33, 23.212.53.178',sig_ids='',sig_names='',date_time='2015-09-11 06:36:47',severity='Informational',attack_type='',geo_location='US',ip_address_intelligence='N/A',username='N/A',session_id='600a4641d5b97f83',src_port='39507',dest_port='80',dest_ip='63.73.131.56',sub_violations='',virus_name='N/A',uri='/Event",
"09 11 2015 06:34:14 32.83.112.225 <LOC0:INFO> Sep 11 06:36:53 macyfw005.fds.com ASM:unit_hostname='macyfw005.fds.com',management_ip_address='32.83.112.225',http_class_name='/Common/GLOBAL-PROTECTION',web_application_name='/Common/GLOBAL-PROTECTION',policy_name='/Common/GLOBAL-PROTECTION',policy_apply_date='2015-08-24 06:28:02',violations='',support_id='2285783706502672559',request_status='passed',response_code='200',ip_client='71.191.129.248',route_domain='0',method='GET',protocol='HTTP',query_string='_fields=name,address,attributes,inventories&productId=2223071',x_forwarded_for_header_value='71.191.129.248, 23.66.230.156',sig_ids='',sig_names='',date_time='2015-09-11 06:36:52',severity='Informational',attack_type='',geo_location='US',ip_address_intelligence='N/A',username='N/A',session_id='553f3abd96644e82',src_port='48270',dest_port='80',dest_ip='63.73.131.56',sub_violations='',virus_name='N/A',uri='/api/store/v2/stores/2455',request='GET /api/store/v2/stores/2455?_fields=name,address,attributes,inventories&productId=2223071 HTTP/1.1\r\nReferer: http://www1.macys.com/shop/product/tahari-a",
"09 11 2015 06:35:43 32.83.112.225 <LOC0:INFO> Sep 11 06:38:21 macyfw005.fds.com ASM:unit_hostname='macyfw005.fds.com',management_ip_address='32.83.112.225',http_class_name='/Common/GLOBAL-PROTECTION',web_application_name='/Common/GLOBAL-PROTECTION',policy_name='/Common/GLOBAL-PROTECTION',policy_apply_date='2015-08-24 06:28:02',violations='',support_id='2285783706541561702',request_status='passed',response_code='302',ip_client='216.52.244.107',route_domain='0',method='GET',protocol='HTTPS',query_string='',x_forwarded_for_header_value='127.0.0.1, 96.6.47.31, 216.52.244.107, 184.87.194.181',sig_ids='',sig_names='',date_time='2015-09-11 06:38:21',severity='Informational',attack_type='',geo_location='US',ip_address_intelligence='N/A',username='N/A',session_id='eebdacc806cb7e4',src_port='52897',dest_port='443',dest_ip='63.73.131.56',sub_violations='',virus_name='N/A',uri='/akamai/akamai-sureroute-test-object.htm',request='GET /akamai/akamai-sureroute-test-object.htm HTTP/1.1\r\nX-Akamai-TestObject: true\r\nPragma: no-cache\r\nUser-Agent: Mashery Proxy\r\nX-Mashery-Message-ID: dc71f4fc-5853-4cb7-9",
];
console.log(array.map(function(s) {
return s.split(',').reduce(function(a, b) {
a[b.split('=')[0]] = b.split('=')[1];
return a;
}, {});
}));
希望有所帮助。