首页 > 代码库 > 正则表达式:将图片的相对网址补全。

正则表达式:将图片的相对网址补全。

Q:

I have an image tag with a src and I want to prepend a website url onto the src but only if it doesn‘t start with http://. so far I have

content.replace(/(<img *src="http://www.mamicode.com/)(.*?)"/, ‘$1‘ + this.websiteUrl + ‘$2"‘);

but I don‘t know how to do the not starting with http:// bit

?

A:

Use a negative lookahead:

content.replace(/(<img *src="http://www.mamicode.com/)(?!http://)(.*?)"/, ‘$1‘ + this.websiteUrl + ‘$2"‘);					

?

my codes:

strText = Regex.Replace(strText, @"(<img.*src="")(?!http:\/\/)(.*?)""", "$1" + siteRootUrl + @"$2""");

?

正则表达式:将图片的相对网址补全。