M
M
Mithgol2011-02-20 19:32:19
JavaScript
Mithgol, 2011-02-20 19:32:19

Regular expressions using lookbehind in JavaScript. Is there any ready-made code that solves this problem once and for all?

As you know, in the JavaScript language you will not be able to simply write something like this in the regular expression language: someString.replace(/(?<!([abcdef]))gh/, …)
It will not work because the Javascript does not take into account the characters that precede the found expression, that is, there is no what is called “lookbehind assertions” - I don’t know if there is Is there a well-established term for this code (“ (?<!……) ”) in Russian.
Naturally, JavaScript programmers have developed a number of tricks to get around this shortcoming. ( Many such tricks are listed over there, for example.) Now
I'm interested in something else, however. Have there been any attempts in the history of open source to finally solve this problem once and for all - for example, by porting the PCRE library to JavaScript in its entirety?
The question may seem rather cheeky in its scope, I know; however, in the end, there have been large-scale (and successful!) attempts to port or emulate JavaScript this and that more than once in history; for example, the Commodore 64 emulator (which I mentioned on Habrahabr last winter), or the open source implementation of many PHP functions in JavaScript, and so on. Suddenly, with PCRE, someone managed to achieve something similar, but I just don’t know?
Again, attempts to add new flags and regular expression elements to JavaScript have also been made - XRegExp , for example. True, there is no lookbehind in it, unfortunately.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoly, 2011-02-21
@taliban

Of course, you can port all this, but then it will be a pointless exercise. It will be so slow that you won't use it. Emulation, in essence, will be a set of indexOf.

H
hayk, 2011-02-21
@hayk

It seems "lookbehind assertions" is "lookback" and "lookahead assertions" is "look ahead".

V
vitaliy2, 2016-11-04
@vitaliy2

Already available with special flags, soon to be available everywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question