我有一个示例http头响应:
HTTP/2 200
content-type: application/json; charset=utf-8
vary: 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: 685
alt-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
我使用这个REGEX:
/^set-cookie:s*([^;]*)/mi
但我得到了:
ds_user=usadaida521;csrftoken=zL2U5EvMG61MNnPaQUGjpK2qklE5uBQ2;rur=FTW;ds_user_id=35321243349;sessionid=35321243349%3ALFbjFj8eRIkr2u%3A21
你能帮我吗?谢谢
最诚挚的问候,欧文
在JavaScript中,我会这样做:
response
.replaceAll(/^(?!set-cookie).*|^set-cookie: /gmi,"")
.trim()
.replaceAll(/[rn]+/g, ';')
样本:
cleanCoockie = response => {
return response
.replaceAll(/^(?!set-cookie).*|^set-cookie: /gmi,"")
.trim()
.replaceAll(/[rn]+/g, ';')
};
$("#result").html(
cleanCoockie("HTTP/2 200 ncontent-type: application/json; charset=utf-8nvary: Cookie, Accept-Language, Accept-Encodingncontent-language: enndate: Wed, 08 Jul 2020 21:20:44 GMTncontent-encoding: gzipnset-cookie: ds_user=usadaida521; Domain=.instagram.com; expires=Tue, 06-Oct-2020 21:20:44 GMT; HttpOnly; Max-Age=7776000; Path=/; Securenset-cookie: csrftoken=ev0sLTzbC4WIIk2FKvGcLLyVqgiFXD3A; Domain=.instagram.com; expires=Wed, 07-Jul-2021 21:20:44 GMT; Max-Age=31449600; Path=/; Securenset-cookie: rur=FTW; Domain=.instagram.com; HttpOnly; Path=/; Securenset-cookie: ds_user_id=35321243349; Domain=.instagram.com; expires=Tue, 06-Oct-2020 21:20:44 GMT; Max-Age=7776000; Path=/; Securenset-cookie: sessionid=35321243349%3AP3jlNxxuIVGoJx%3A24; Domain=.instagram.com; expires=Thu, 08-Jul-2021 21:20:44 GMT; HttpOnly; Max-Age=31536000; Path=/; Securencontent-length: 685nalt-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>