Jefferson
Jefferson

Reputation: 197

Javascript cookie.match

I have a cookie that I am trying to match it works fine on the online tool but not in the Chrome console using the [\w+]. Any help would be great.

cookie Name customerid value =2488475 

In Chrome console, I am trying document.cookie.match('customerid=document.cookie.match('customerid=([\w+])*'); but it is returning null

Upvotes: 0

Views: 314

Answers (1)

XMehdi01
XMehdi01

Reputation: 1

As Barmar said when using Regular expressions match method take //, not ''.

follow pattern string.match(regexp), So change single quotes with slash

document.cookie.match('customerid=[\w+]');
//                    ^                ^

Note: The match() method returns the matches in an array and null if no match is found.

To get a value from cookie with regex Ref:

window.getCookie = function(name) {
  var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
  if (match) return match[2];
}

In your case call the function getCookie('customerid')

Upvotes: 1

Related Questions