要取到cookie=,CompanyName=,emp_session=后面的值。
日志数据:(由于格式关系,日志在一行里。)
192.168.1.105 - - [19/Dec/2017:16:54:11 +0800] "GET /index.php HTTP/1.1" 200 51315 "http://192.168.1.180:808
0/?redirect_url=http%3A%2F%2F192.168.1.180%2Findex.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKi
t/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "_ati=7702190658068; _uab_collina=151366420
445546020031294; _umdata=2FB0BDB3C12E491D8C00966F97B3386E7E149E5C1254FEFA3E19338B3D92CA3E1252BC2B8957B853CD4
3AD3E795C914CA75E9DDEEACFC78F4A13336CC01A65F6; cookie=3df396c7.560ad17f09d46; CompanyName=%E4%B8%8A%E6%B5%
B7%E6%96%B0%E5%AE%9E%E6%95%B0%E7%A0%81%E7%A7%91%E6%8A%80%E8%82%A1%E4%BB%BD%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%
B8; emp_session=c8621872d2fd5bdf61f9aee06e80231e543b7d6a"
192.168.1.105 - - [19/Dec/2017:16:55:18 +0800] "GET /index.php HTTP/1.1" 200 51315 "http://192.168.1.180:808
0/?redirect_url=http%3A%2F%2F192.168.1.180%2Findex.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKi
t/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "_ati=7702190658068; _uab_collina=151366420
445546020031294; _umdata=2FB0BDB3C12E491D8C00966F97B3386E7E149E5C1254FEFA3E19338B3D92CA3E1252BC2B8957B853CD4
3AD3E795C914CA75E9DDEEACFC78F4A13336CC01A65F6; cookie=3df396c7.560ad17f09d46; CompanyName=HJ; emp_session=c8621872d2fd5bdf61f9aee06e80231e543b7d6a"
var s = 'your log text'; var re = /cookie=([^;]*;)|CompanyName=([^;]*;)|emp_session=([^;"]*)/ig; console.log(s.match(re)); //["cookie=3df396c7.560ad17f09d46;", "CompanyName=%E4%B8%8A%E6%B5%B7%E6%96%B0%E5%AE%9E%E6%95%B0%E7%A0%81%E7%A7%91%E6%8A%80%E8%82%A1%E4%BB%BD%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8;", "emp_session=c8621872d2fd5bdf61f9aee06e80231e543b7d6a", "cookie=3df396c7.560ad17f09d46;", "CompanyName=HJ;", "emp_session=c8621872d2fd5bdf61f9aee06e80231e543b7d6a"]
最后用字符串方法分析一下这个数组就可以了。
没想到这个[^;]
可以可以的。点个赞
@BUTTERAPPLE: 刚才写得太匆忙,还可以简化一下:
(cookie|CompanyName|emp_session)=([^;"]*)
写了一个比较丑的,但是能匹配
cookie=(.*);.C|ompanyName=(.*);|emp_session=(.*)"
这能把所有Cookie empSession 以及CompanyName 都匹配出来,
匹配的结果为
cookie=3df396c7.560ad17f09d46; CompanyName=%E4%B8%8A%E6%B5%B7%E6%96%B0%E5%AE%9E%E6%95%B0%E7%A0%81%E7%A7%91%E6%8A%80%E8%82%A1%E4%BB%BD%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8;emp_session=c8621872d2fd5bdf61f9aee06e80231e543b7d6a"cookie=3df396c7.560ad17f09d46; CompanyName=HJ;emp_session=c8621872d2fd5bdf61f9aee06e80231e543b7d6a"
通过$1 $2 $3 能够分别取出各自对应的值
$1
3df396c7.560ad17f09d463df396c7.560ad17f09d46
$2
%E4%B8%8A%E6%B5%B7%E6%96%B0%E5%AE%9E%E6%95%B0%E7%A0%81%E7%A7%91%E6%8A%80%E8%82%A1%E4%BB%BD%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8HJ
$3
c8621872d2fd5bdf61f9aee06e80231e543b7d6ac8621872d2fd5bdf61f9aee06e80231e543b7d6a