I have the following code:
var requestData = {};
var byPattern = /by=(\w+)/;
var value = byPattern.exec(stringToSearch);
if (value && value.length === 2)
requestData.by = value[1];
The first problem with this regex, was that if stringToSearch is for example “standby=foo”, it matched and returned “foo”. I want it to fail there. I want the regex to match only if there is nothing before “by”, or spaces.
So I replaced by /^\s*by=(\w+)/
That’s better, but I want the regex to match if stringToSearch is for example “city=paris by=foo”. It should match and return “foo”. Not the case here :/
Can someone help me fix the regex? Thanks a lot!
If you want spaces or nothing before the capturing group, that would be
( +|^):Technically, I’m matching space or nothing, but the effect is the same.