John
John

Reputation: 95

Regex match cookie value and remove hyphens

I'm trying to extract out a group of words from a larger string/cookie that are separated by hyphens. I would like to replace the hyphens with a space and set to a variable. Javascript or jQuery.

As an example, the larger string has a name and value like this within it:

facility=34222%7CConner-Department-Store;

(notice the leading "C")

So first, I need to match()/find facility=34222%7CConner-Department-Store; with regex. Then break it down to "Conner Department Store"

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

Upvotes: 1

Views: 6245

Answers (4)

ndeufemia
ndeufemia

Reputation: 474

Ok, first, you should decode this string as follows:

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

Then you have multiple possibilities to split up this string. The easiest way is to use substring()

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

As you can see, substring(arg1, arg2) returns the string, starting at index arg1 and ending at index arg2. See Full Documentation here

If you want to cut the last ; just set decoded.length - 1 as arg2 in the snippet above.

decoded.substring(decoded.indexOf('|') + 1, decoded.length - 1)
//returns "Conner-Department-Store"
  • or all above in just one line:

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

If you want still to use a regular Expression to retrieve (perhaps more) data out of the string, you could use something similar to this snippet:

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"
}

I have applied some rules for the regex to work, feel free to modify them according your needs. facility can be any Word built with alphabetical characters lower and uppercase (no other chars) at any length = needs to be the char = 34222 can be any number but no other characters | needs to be the char | Conner-Department-Store can be any characters except one of the following (reserved delimiters): :/?#[]@;,'

Hope this helps :)

edit: to find only the part facility=34222%7CConner-Department-Store; just modify the regex to match facility= instead of ([A-z]*)=: /(facility)=([0-9]*)\|(\S[^:\/?#\[\]\@\;\,']*)/

Upvotes: 0

cнŝdk
cнŝdk

Reputation: 32145

If I understood it correctly, you want to make a phrase by getting all the words between hyphens and disallowing two successive Uppercase letters in a word, so I'd prefer using Regex in that case.

This is a Regex solution, that works dynamically with any cookies in the same format and extract the wanted sentence from it:

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

Demo:

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(" "));

Explanation:

  • Use this Regex (/([A-Z][a-z]+)-?/g to match the words between -.
  • Replace any - occurence in the matched words.
  • Then just join these matches array with white space.

Upvotes: 1

Taplar
Taplar

Reputation: 24965

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

Upvotes: 3

Lincoln Bergeson
Lincoln Bergeson

Reputation: 3451

You can use cookies.js, a mini framework from MDN (Mozilla Developer Network).

Simply include the cookies.js file in your application, and write:

docCookies.getItem("Connor Department Store");

Upvotes: -1

Related Questions