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

时间:2017-06-06 22:51:51

标签: javascript jquery regex cookies match

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

例如,较大的字符串在其中具有如下名称和值:

facility=34222%7CConner-Department-Store;

(注意领先的“C”)

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

var cookie = document.cookie; var facilityValue = cookie.match(REGEX); ??

4 个答案:

答案 0 :(得分:3)



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, ' ') );




答案 1 :(得分:1)

如果我理解正确,你想通过连字符之间的所有单词和一个单词中不允许两个连续的大写字母来创建一个短语,所以我更喜欢在这种情况下使用正则表达式。

这是一个正则表达式解决方案,可以与任何相同格式的Cookie动态协作,并从中提取所需的句子:

var matches = str.match(/([A-Z][a-z]+)-?/g);
console.log(matches.map(function(m) {
  return m.replace('-', '');
}).join(" "));

<强>演示:

var str = "facility=34222%7CConner-Department-Store;";

var matches = str.match(/([A-Z][a-z]+)-?/g);
console.log(matches.map(function(m) {
  return m.replace('-', '');
}).join(" "));

<强>解释

  • 使用此正则表达式(/([A-Z][a-z]+)-?/g匹配-
  • 之间的字词
  • 替换匹配字词中的任何-个出现。
  • 然后只需将这些匹配数组与空格连接即可。

答案 2 :(得分:0)

确定, 首先,您应该按如下方式解码此字符串:

var str = "facility=34222%7CConner-Department-Store;"
var decoded = decodeURIComponent(str);
// decoded = "facility=34222|Conner-Department-Store;"

然后你有多种可能性来分割这个字符串。 最简单的方法是使用substring()

var solution1 = decoded.substring(decoded.indexOf('|') + 1, decoded.length) // solution1 = "Conner-Department-Store;" solution1 = solution1.replace('-', ' '); // solution1 = "Conner Department Store;"

如您所见,substring(arg1, arg2)返回字符串,从索引arg1开始,到索引arg2结束。请参阅完整文档here

如果您想在上面的代码段中剪切最后;仅将decoded.length - 1设为arg2

decoded.substring(decoded.indexOf('|') + 1, decoded.length - 1)
//returns "Conner-Department-Store"
  • 或以上只有一行:

decoded.substring(decoded.indexOf('|') + 1, decoded.length - 1).replace('-', ' ')

如果您仍想使用正则表达式从字符串中检索(可能更多)数据,您可以使用类似于此代码段的内容:

var solution2 = "";
var regEx= /([A-Za-z]*)=([0-9]*)\|(\S[^:\/?#\[\]\@\;\,']*)/;
if (regEx.test(decoded)) {
solution2 = decoded.match(regEx);
    /* returns 
    [0:"facility=34222|Conner-Department-Store",
    1:"facility",
    2:"34222",
    3:"Conner-Department-Store",
    index:0,
    input:"facility=34222|Conner-Department-Store;"
    length:4] */
solution2 = solution2[3].replace('-', ' ');
// "Conner Department Store"
}

我已经为正则表达式应用了一些规则,可以根据需要随意修改它们。 facility可以是任何长度的字母字符大小写(没有其他字符)构建的任何Word =需要成为char = 34222可以是任意数字,但不能是其他字符 |需要成为char | Conner-Department-Store可以是除以下之一之外的任何字符(保留分隔符)::/?#[]@;,'

希望这会有所帮助:)

  

编辑:仅查找部分   facility=34222%7CConner-Department-Store;只需将正则表达式修改为   匹配facility=而不是([A-z]*)=:   /(facility)=([0-9]*)\|(\S[^:\/?#\[\]\@\;\,']*)/

答案 3 :(得分:-1)

您可以使用来自MDN(Mozilla开发者网络)的迷你框架cookies.js

只需在您的应用程序中包含cookies.js文件,然后写下:

docCookies.getItem("Connor Department Store");