rplaurindo rplaurindo - 5 months ago 12
Javascript Question

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


Answer

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

The final regular expression was thus:

\b((foo\=[^&#]*)|(foo(?=[&#]))|([^#]foo(?!.)))

The especial character \b match a boundary word, then, for example, doesn't match: "nonfoo=bar", but match "foo", "foo=bar" and doesn't include nothing before "foo" (works it as positive lookbehind).

Comments