javascript - get specific value/params (keypare) from string -
i have dynamic string.
[ "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", ];
and want
- management_ip_address
- user - agent
- request
tried
var match, pl = /\+/g, // regex replacing addition symbol 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; }
and
function getparams(varstring) { var regex = new regexp("[\\?&] =([^&#]*)"), results = regex.exec(varstring); return results === null ? "" : decodeuricomponent(results[1].replace(/\+/g, " ")); }
i need output :
{ "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" }
this should work you:
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, ..}]
we map on strings (replace them) reduce gets text before '=' , text after '=' every string within ','.
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; }, {}); }));
hope helps.
Comments
Post a Comment