gpt4 book ai didi

javascript - 正则表达式匹配 cookie 值并删除连字符

转载 作者:行者123 更新时间:2023-11-30 11:35:58 25 4
gpt4 key购买 nike

我正在尝试从一个较大的字符串/cookie 中提取一组由连字符分隔的单词。我想用空格替换连字符并设置为变量。 Javascript 或 jQuery。

例如,较大的字符串中有这样的名称和值:

facility=34222%7CConner-Department-Store;

(注意前导“C”)

所以首先,我需要 match()/find facility=34222%7CConner-Department-Store;与正则表达式。然后分解为 "Conner Department Store"

var cookie = document.cookie;var facilityValue = cookie.match(正则表达式); ??

最佳答案

var test = "store=874635%7Csomethingelse;facility=34222%7CConner-Department-Store;store=874635%7Csomethingelse;";

var test2 = test.replace(/^(.*)facility=([^;]+)(.*)$/, function(matchedString, match1, match2, match3){
return decodeURIComponent(match2);
});

console.log( test2 );
console.log( test2.split('|')[1].replace(/[-]/g, ' ') );

关于javascript - 正则表达式匹配 cookie 值并删除连字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44400900/

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