猿问

正则表达式 HTTP 响应标头

我有一个 http 标头响应示例:

HTTP/2 200 content-type: application/json; charset=utf-8vary: Cookie, Accept-Language, Accept-Encoding
content-language: en
date: Wed, 08 Jul 2020 21:20:44 GMT
content-encoding: gzip
set-cookie: ds_user=usadaida521; Domain=.instagram.com; expires=Tue, 06-Oct-2020 21:20:44 GMT; HttpOnly; Max-Age=7776000; Path=/; Secure
set-cookie: csrftoken=ev0sLTzbC4WIIk2FKvGcLLyVqgiFXD3A; Domain=.instagram.com; expires=Wed, 07-Jul-2021 21:20:44 GMT; Max-Age=31449600; Path=/; Secure
set-cookie: rur=FTW; Domain=.instagram.com; HttpOnly; Path=/; Secure
set-cookie: ds_user_id=35321243349; Domain=.instagram.com; expires=Tue, 06-Oct-2020 21:20:44 GMT; Max-Age=7776000; Path=/; Secure
set-cookie: sessionid=35321243349%3AP3jlNxxuIVGoJx%3A24; Domain=.instagram.com; expires=Thu, 08-Jul-2021 21:20:44 GMT; HttpOnly; Max-Age=31536000; Path=/; Secure
content-length: 685alt-svc: h3-29=":443"; ma=3600,h3-27=":443"; ma=3600

我需要得到的是:

ds_user=usadaida521; Domain=.instagram.com; expires=Tue, 06-Oct-2020 21:20:44 GMT; HttpOnly; Max-Age=7776000; Path=/; Secure;csrftoken=ev0sLTzbC4WIIk2FKvGcLLyVqgiFXD3A; Domain=.instagram.com; expires=Wed, 07-Jul-2021 21:20:44 GMT; Max-Age=31449600; Path=/; Secure;rur=FTW; Domain=.instagram.com; HttpOnly; Path=/; Secure;ds_user_id=35321243349; Domain=.instagram.com; expires=Tue, 06-Oct-2020 21:20:44 GMT; Max-Age=7776000; Path=/; Secure;sessionid=35321243349%3AP3jlNxxuIVGoJx%3A24; Domain=.instagram.com; expires=Thu, 08-Jul-2021 21:20:44 GMT; HttpOnly; Max-Age=31536000; Path=/; Secure

我使用这个正则表达式:

/^set-cookie:\s*([^;]*)/mi

但我得到:

ds_user=usadaida521;csrftoken=zL2U5EvMG61MNnPaQUGjpK2qklE5uBQ2;rur=FTW;ds_user_id=35321243349;sessionid=35321243349%3ALFbjFj8eRIkr2u%3A21

你能帮我吗?谢谢你

阿晨1998
浏览 132回答 1
1回答

30秒到达战场

在 JavaScript 中我会这样做:response&nbsp; &nbsp; .replaceAll(/^(?!set-cookie).*|^set-cookie: /gmi,"")&nbsp; &nbsp; .trim()&nbsp; &nbsp; .replaceAll(/[\r\n]+/g, ';')样本:cleanCoockie = response => {&nbsp; &nbsp; return response&nbsp; &nbsp; &nbsp; &nbsp; .replaceAll(/^(?!set-cookie).*|^set-cookie: /gmi,"")&nbsp; &nbsp; &nbsp; &nbsp; .trim()&nbsp; &nbsp; &nbsp; &nbsp; .replaceAll(/[\r\n]+/g, ';')};$("#result").html(&nbsp; &nbsp; cleanCoockie("HTTP/2 200 \ncontent-type: application/json; charset=utf-8\nvary: Cookie, Accept-Language, Accept-Encoding\ncontent-language: en\ndate: Wed, 08 Jul 2020 21:20:44 GMT\ncontent-encoding: gzip\nset-cookie: ds_user=usadaida521; Domain=.instagram.com; expires=Tue, 06-Oct-2020 21:20:44 GMT; HttpOnly; Max-Age=7776000; Path=/; Secure\nset-cookie: csrftoken=ev0sLTzbC4WIIk2FKvGcLLyVqgiFXD3A; Domain=.instagram.com; expires=Wed, 07-Jul-2021 21:20:44 GMT; Max-Age=31449600; Path=/; Secure\nset-cookie: rur=FTW; Domain=.instagram.com; HttpOnly; Path=/; Secure\nset-cookie: ds_user_id=35321243349; Domain=.instagram.com; expires=Tue, 06-Oct-2020 21:20:44 GMT; Max-Age=7776000; Path=/; Secure\nset-cookie: sessionid=35321243349%3AP3jlNxxuIVGoJx%3A24; Domain=.instagram.com; expires=Thu, 08-Jul-2021 21:20:44 GMT; HttpOnly; Max-Age=31536000; Path=/; Secure\ncontent-length: 685\nalt-svc: h3-29=\":443\"; ma=3600,h3-27=\":443\"; ma=3600"));<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><textarea id="result" rows="9" cols="80"></textarea>
随时随地看视频慕课网APP
我要回答