gpt4 book ai didi

javascript - 此 cookie 值的正则表达式?

转载 作者:行者123 更新时间:2023-11-30 07:55:49 26 4
gpt4 key购买 nike

我有一个长值的 cookie,我在下面粘贴了一个示例。我需要拆分名称值对并返回,在本例中为“id”,它从下面对应于 167814329

我无法阅读开发人员在这里使用的语法(我是一名分析师,而不是开发工程师)。在过去,我看到过与 = 或 '|' 一起使用的名称值对作为分隔符的语法。

看起来名字值对是这样的:

%22id%22%3A167814329%2C

var usd = document.cookie.match(/(^| )user_session_data\=([^;]*)(;|$)/)[0]
var arr = usd.split('%22')
var ind = arr.indexOf('id');
var pair = arr[ind];

但是 pair 只等于“id”,我希望得到这个数组值 %22id%22%3A167814329%2C 然后我可以拆分。

获得 167814329 的干净正则表达式是什么?

这是要搜索的示例 cookie 值:

user_session_data=%7B%22lastUpdated%22%3A1476155189.373%2C%22active%22%3Afalse%2C%22uType%22%3A%22none%22%2C%22entitled%22%3Afalse%2C%22gift%22%3Afalse%2C%22stacked%22%3A%22none%22%2C%22usedsomestringFreeTrial%22%3A%22none%22%2C%22cancelled%22%3A%22none%22%2C%22displayName%22%3A%2220161010_gpc%40example.com%22%2C%22email%22%3A%2220161010_gpc%40example.com%22%2C%22userName%22%3A%2220161010_gpc%40example.com%22%2C%22firstName%22%3Anull%2C%22salesChannel%22%3A%22cat%22%2C%22marketingOptIn%22%3Atrue%2C%22newIssueEmailNotifyEnabled%22%3Afalse%2C%22restartSubscriptions%22%3A%5B%5D%2C%22id%22%3A167814329%2C%22purchaseType%22%3A%22NONE%22%2C%22futurePurchaseType%22%3A%22NONE%22%2C%22somestringStatus%22%3Anull%2C%22hasFuturePrivilege%22%3Afalse%2C%22rogersUser%22%3Afalse%2C%22suspendedStatus%22%3Anull%2C%22socialEmail%22%3A%22%22%2C%22somestringOffersStale%22%3Anull%2C%22somestringcabbagesPrice%22%3Anull%2C%22somestringcabbagesId%22%3Anull%2C%22somestringcabbagesFreeTrial%22%3Anull%2C%22somestringcabbagesOfferPackageId%22%3Anull%2C%22somestringcarrotsPrice%22%3Anull%2C%22somestringcarrotsId%22%3Anull%2C%22somestringcarrotsFreeTrial%22%3Anull%2C%22somestringcarrotsOfferPackageId%22%3Anull%2C%22launchedFromReader%22%3Anull%2C%22userPaymentMethod%22%3Anull%2C%22iseligibleforfreetrial%22%3Atrue%2C%22loginTimeStamp%22%3A%22Tue%20Oct%2011%202016%2011%3A06%3A29%20GMT%2B0800%20(PHT)%22%2C%22latestSubscriptionSource%22%3Anull%7D;"

最佳答案

似乎如果你删除开头的 user_session_data 和结尾的 ;,你就得到了一个转义字符串。你可以unescape它,并像这样获取您的 ID(它实际上是一个 JSON 对象):

var obj = "%7B%22lastUpdated%22%3A1476155189.373%2C%22active%22%3Afalse%2C%22uType%22%3A%22none%22%2C%22entitled%22%3Afalse%2C%22gift%22%3Afalse%2C%22stacked%22%3A%22none%22%2C%22usedsomestringFreeTrial%22%3A%22none%22%2C%22cancelled%22%3A%22none%22%2C%22displayName%22%3A%2220161010_gpc%40example.com%22%2C%22email%22%3A%2220161010_gpc%40example.com%22%2C%22userName%22%3A%2220161010_gpc%40example.com%22%2C%22firstName%22%3Anull%2C%22salesChannel%22%3A%22cat%22%2C%22marketingOptIn%22%3Atrue%2C%22newIssueEmailNotifyEnabled%22%3Afalse%2C%22restartSubscriptions%22%3A%5B%5D%2C%22id%22%3A167814329%2C%22purchaseType%22%3A%22NONE%22%2C%22futurePurchaseType%22%3A%22NONE%22%2C%22somestringStatus%22%3Anull%2C%22hasFuturePrivilege%22%3Afalse%2C%22rogersUser%22%3Afalse%2C%22suspendedStatus%22%3Anull%2C%22socialEmail%22%3A%22%22%2C%22somestringOffersStale%22%3Anull%2C%22somestringcabbagesPrice%22%3Anull%2C%22somestringcabbagesId%22%3Anull%2C%22somestringcabbagesFreeTrial%22%3Anull%2C%22somestringcabbagesOfferPackageId%22%3Anull%2C%22somestringcarrotsPrice%22%3Anull%2C%22somestringcarrotsId%22%3Anull%2C%22somestringcarrotsFreeTrial%22%3Anull%2C%22somestringcarrotsOfferPackageId%22%3Anull%2C%22launchedFromReader%22%3Anull%2C%22userPaymentMethod%22%3Anull%2C%22iseligibleforfreetrial%22%3Atrue%2C%22loginTimeStamp%22%3A%22Tue%20Oct%2011%202016%2011%3A06%3A29%20GMT%2B0800%20(PHT)%22%2C%22latestSubscriptionSource%22%3Anull%7D"

var myObject = JSON.parse(unescape(obj));
console.log(myObject.id); //167814329

JSFiddle:https://jsfiddle.net/orrt86cp/

您的 JSON 如下所示:

{  
"lastUpdated":1476155189.373,
"active":false,
"uType":"none",
"entitled":false,
"gift":false,
"stacked":"none",
"usedsomestringFreeTrial":"none",
"cancelled":"none",
"displayName":"20161010_gpc@example.com",
"email":"20161010_gpc@example.com",
"userName":"20161010_gpc@example.com",
"firstName":null,
"salesChannel":"cat",
"marketingOptIn":true,
"newIssueEmailNotifyEnabled":false,
"restartSubscriptions":[

],
"id":167814329,
"purchaseType":"NONE",
"futurePurchaseType":"NONE",
"somestringStatus":null,
"hasFuturePrivilege":false,
"rogersUser":false,
"suspendedStatus":null,
"socialEmail":"",
"somestringOffersStale":null,
"somestringcabbagesPrice":null,
"somestringcabbagesId":null,
"somestringcabbagesFreeTrial":null,
"somestringcabbagesOfferPackageId":null,
"somestringcarrotsPrice":null,
"somestringcarrotsId":null,
"somestringcarrotsFreeTrial":null,
"somestringcarrotsOfferPackageId":null,
"launchedFromReader":null,
"userPaymentMethod":null,
"iseligibleforfreetrial":true,
"loginTimeStamp":"Tue Oct 11 2016 11:06:29 GMT+0800 (PHT)",
"latestSubscriptionSource":null
}

关于javascript - 此 cookie 值的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39970659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com