作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有动态字符串。
[
"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"
}
最佳答案
这应该适合你:
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 映射所有字符串(替换它们),该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;
}, {});
}));
希望有帮助。
关于javascript - 从字符串中获取特定值/参数(keypare),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863788/
我有动态字符串。 [ "09 11 2015 06:34:09 32.83.112.225 Sep 11 06:36:48 macyfw005.fds.com ASM:unit_hostna
我是一名优秀的程序员,十分优秀!