0

Im trying to split cookies into gropus using this regex (.*?=.*?); ? What i meant to get is a cookie in each regex group. But it's only giving me the first group that match.

var string = userid=edaa4200-b140-47fd-9db8-b48082a0d0a8; ab_93896527=0;

edit : I'm using exec() function

var cookies_tuples = /(.*?=.*?); ?/g.exec(string);

what could be the problem ?

limitless
  • 669
  • 7
  • 18

3 Answers3

2

you have to call exec multiple times, even with the global flag enabled:

    var re = /(.*?=.*?);\s?/g;
    var input = "userid=edaa4200-b140-47fd-9db8-b48082a0d0a8; ab_93896527=0;";
    var myArray;
    while ((myArray = re.exec(input)) != null)
    {
      var msg = "Found " + myArray[0];
      console.log(msg);
    }
Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
1

Just use the string.match method as indicated below. If you use RegEx.exec you'll need to iterate to get all the matches.

var s = "userid=edaa4200-b140-47fd-9db8-b48082a0d0a8; ab_93896527=0;"
var cookies_tuples = s.match(/(.*?=.*?); ?/g);

console.log(cookies_tuples);
Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
0

in https://regex101.com/ it gives me both cookies. You havo to run it greedy <- with the g-flag

  • and bevor anyone can complain, you need 50 Reputation to comment, so I just wrote an answer ;)
Maria
  • 151
  • 2
  • 9