Search code examples
javascriptregexlookbehind

How to implement positive "lookbehind" of regular expressions in Javascript


UPDATED QUESTION

Suppose the string "?foo=bar&nonfoo=bar&foo=bar", i need capture in this case:

  • foo=bar
  • foo=foo

I did with Perl, see here.

But the Javascript doesn't support lookbehind, then the expression (?<=) (positive lookbehind) doesn't recognized.

I try too (?:[?&])((foo\=[^&#]*)|(foo(?=[&#]))|(foo(?!.))) (non capturing group syntax), but to execute the method match is returned:

  • ?foo=bar
  • &foo=foo

Solution

  • The solution was in our face. Sorry for my late.

    The final regular expression was thus:

    \b(foo\b\=[^&#]*)
    

    The especial character \b set a boundary word for matching, then, for example, it doesn't match "nonfoo=bar", but match "foo", "foo=bar" and doesn't include nothing before "foo" (works it as positive lookbehind), but if the word is after the URI Hash it is also matched (for now there is no way to solve this).