ScottHamper / Cookies

JavaScript Client-Side Cookie Manipulation Library
The Unlicense
1.77k stars 169 forks source link

Strange regexp replace #80

Closed LastRide closed 5 years ago

LastRide commented 6 years ago

Hi.

Could you explain please next string of code?

value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);

If value would be json - '[{"name":"test"}]' would be => '[{%22name%22:%22test%22}]'

ScottHamper commented 6 years ago

Hey @LastRide ,

The regex is used to replace all characters in the cookie value that are illegal, according to RFC6265, by replacing them with a URI encoded representation (percent encoding).

See the "A Note About Encoding" section of the README for more info.