我有一个长值的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];
但是对等于" 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;"
答案 0 :(得分:4)
似乎如果你删除了开头的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
}
答案 1 :(得分:2)
HanletEscaño肯定有最准确的答案,但是如果你真的想要使用正则表达式,你可以。目前还不清楚你是想要id%22%3a[ID]%2C
还是仅仅是ID,所以这里有两个:
完整字符串:(?:^| )user_session_data\=[^;]*(id%22%3A\d+%2C)[^;]*(?:;|$)
只是ID:(?:^| )user_session_data\=[^;]*id%22%3A(\d+)%2C[^;]*(?:;|$)
将来,如果您在使用正则表达式时遇到问题,this site可能非常有用。