Simple question how to use a variable in a javascript regular expression?

I tried:

```
var regex = new RegExp('/^' + name + '/');
var result = cookie.name.match(regex);

This does not work - if I debug the var regex I get:

/\/^foobar\//

```

For the record I am expecting to match a cookie named foobar\_xxxxxxxx

Something so simple is somehow so challenging? I've seen numerous other posts asking the same question without a satisfactory answer that works in my case.