Negative Lookbehind JavaScript

[亡魂溺海] 提交于 2019-12-11 09:16:52

问题


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 want the g flag The regex I've made is:

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

This works fine when negative look-behind is supported. But when I do it in JavaScript. It doesn't support negative look behind. Here's the part where the problem lies:

(?:(?<!\\)\}\:end)

What it should match:

\}:end    <- Not this
}:end     <- This
foo}:end  <- This

I've tried:

  • Javascript: negative lookbehind equivalent?
  • JavaScript negative lookbehind issue

回答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




回答2:


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

Try this, see the demo:

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



来源:https://stackoverflow.com/questions/30005902/negative-lookbehind-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!