Skip to content Skip to sidebar Skip to footer

Negative Lookbehind Javascript

I know that a lot of people have posted a similar question but with the g flag, none of the 'hacks' seem to work for me properly (meaning they don't work most of the time). Yes I w

Solution 1:

Instead using lookbehind you can use lookahead

e.g.

abcdefg;}hijklmn#}

to get

$1 = `abcdefg;}hijklmn`
$2 = `#}`

using lookbehind approach you can do

(.*)(?<!#})(#})

see: DEMO (lookbehind approach)

which here prevent a duplication of #} by using negative lookbehind.

On the other hand, lookahead approach is also possible by "spawning lookahead",

let's consider our example text

abcdefg;}hijklmn#}

become

%a%b%c%d%e%f%g%;%}%h%i%j%k%l%m%n%#}

here %s represent to lookahead.

How we can spawn lookahead like that?

Let's consider lookahead approach regex for our example text

((?:(?!#}).)*)(#})

see: DEMO (lookahead approach)

A trick here is spawning lookahead together with . where the regex ((?:(?!#}).)*) can be expanded to

((?!#}).(?!#}).(?!#}).(?!#}).(?!#}).(?!#}).) and so on

that means it will check for every letters to guarantee you that there is no #} in (.*).

Thus, if apply this strategy to your regex you will get

\~\![A-Za-z]+\ start(?:(?:(?:\(|,)\ ?[\w]+)*?\))\:\{((?:(?!\}\:end\n)[\S\s])+)(?:\}\:end)

see: DEMO


Solution 2:

\~\![A-Za-z]+\ start(?:(?:(?:\(|,)\ ?[\w]+)*?\))\:\{([\S\s]+?)(?:[^\\]\}\:end)

Try this, see the demo:

https://regex101.com/r/uE3cC4/17


Post a Comment for "Negative Lookbehind Javascript"