javascript - 从字符串中获取特定值/参数(keypare)

标签 javascript jquery regex

我有动态字符串。

    [
"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",
        ];

我想要得到

  1. management_ip_address
  2. 用户 - 代理
  3. 请求

尝试过

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/

相关文章:

特定数字的正则表达式,忽略其他数字

MySQL 正则表达式 + 空格 (\s)

javascript - 用于匹配两个标签之间的文本的正则表达式

javascript - 在项目目录中找不到 ffmpeg.exe

Javascript:有没有办法为 Parse.Promise.when 指定无限输入参数?

javascript - Jquery.change 在页面加载时触发

javascript - 是 switch case 还是动画方法?

javascript - 这是使用 jQuery 对 HTML 重新排序的有效方法吗?

javascript - 根据 typescript 中的 'in'条件过滤数组元素

javascript - Angular promise : get data from buffer if available