Home  >  Article  >  Web Front-end  >  Replace image address img tag with regular expression_Basic knowledge

Replace image address img tag with regular expression_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:13:051645browse

The solution I initially thought of is:

Copy code The code is as follows:

content.replace(/ ]*src=['"]([^'"] )[^>]*>/gi, function (match) {
console.log(match);
});

The output result is:

Copy code The code is as follows:


What I get is the entire img tag, but what I expect is the URL in src, so I just need to return the new address in function(match).
So, I’m stuck here. . .
Later, I searched Google for the keyword "javascript replace callback" and found "replace callback function with matches" in stackoverflow, and then I learned that function (match) has other parameters

Then, change to the code below and the problem will be solved.

Copy code The code is as follows:

content.replace(/ ]*src=['"]([^'"] )[^>]*>/gi, function (match, capture) {
console.log(capture);
});

Output result:

Copy code The code is as follows:

http://www.jb51.net/images/ logo.gif
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn