跳到主要内容
版本:0.17.0+

regex-howto

lookahead / lookbehind

conclusion


A(?=B) # positive lookahead
A(?!B) # negative lookahead

(?<=A)B # positive lookbehind
(?<!A)B # negative lookbehind

So, the = means positive, and the ! means negative, the ? means look, the < means back.

ref:

detail

I want to match the url in the image tag, but except those online ones.

There are quite a lot ways to help me finish this.

For example, I first matched an img, and extract the url, then test if the url starts with http.

But it's a bit slow, and needs a little more codes. (i.e. not atomic)

I also came up with an 折中的 idea, i.e. just to match those start with '.', which indicates a local url.

picture 3

However, I want a step further.

I know then it's so-called lookahead or lookbehind.

picture 4

So I did a quite hard search, and finally I derived my answer from here:

picture 5

And then I am surprised to find what I wanna to realize can be easily finished via look-ahead based on src=" rather than look-behind based on a ANYTHING.

How 巧妙的 this is!

picture 2

core ref:

Thanks for YuanHao

ref:

it seems ok but indeed not.

A detailed tutorial of a professional regex website