开发者

Is there any way of using the not (^) in regex for multiple characters?

开发者 https://www.devze.com 2023-02-14 11:31 出处:网络
I want to make a Regex pattern that matches all relative patches. What i want to match: img src=\"image.png\"

I want to make a Regex pattern that matches all relative patches.

What i want to match:

img src="image.png"
img src="http_image.png"
开发者_如何转开发

What i don't want to match:

img src="http://example/image.png"

I tried matching with these patterns, but none of them work:

\src="[^http://]\
\src="^(http://)\
\src="[^h][^t][^t][^p][^:][^/][^/]\
\src="([^h][^t][^t][^p][^:][^/][^/])\

I leaved the <> out the img-tag because else i couldn't write it as code.

The src attribute will always be formatted with double-quotes (") not single-quotes (').

It will always contain a "http" source, not "https" or others.


The way to solve this is by using negative lookahead assertion:

^img src="(?!http:\/\/\S+).*"$

Rubular link

^                  : Start anchor
img src="          : Literal img src="
(?!http:\/\/\S+)   : To ensure the string following " is not a URL
.*                 : Anything else is allowed
"                  : Literal "
$                  : End anchor
0

精彩评论

暂无评论...
验证码 换一张
取 消