为了账号安全,请及时绑定邮箱和手机立即绑定

正则表达式 HTTP 响应标头

正则表达式 HTTP 响应标头

PHP
阿晨1998 2023-07-21 16:20:27
我有一个 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你能帮我吗?谢谢你
查看完整描述

1 回答

?
30秒到达战场

TA贡献1828条经验 获得超6个赞

在 JavaScript 中我会这样做:


response

    .replaceAll(/^(?!set-cookie).*|^set-cookie: /gmi,"")

    .trim()

    .replaceAll(/[\r\n]+/g, ';')

样本:


cleanCoockie = response => {

    return response

        .replaceAll(/^(?!set-cookie).*|^set-cookie: /gmi,"")

        .trim()

        .replaceAll(/[\r\n]+/g, ';')

};


$("#result").html(

    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>


查看完整回答
反对 回复 2023-07-21
  • 1 回答
  • 0 关注
  • 131 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信