A problem was encountered today in development: how do I replace the SRC values of all the IMG tags contained in an HTML string?
The solution that comes to mind is:
Content.replace (/]*src=[']] ([^ ' "]+) [^>]*>/gi, function (match) {
Console.log (match);
});
The output results are:
Get the entire IMG tag, but I'm expecting a URL in src so I can just return the new address in the function (match).
So, it's stuck here ...
Later, through google search keyword "javascript replace callback", found in StackOverflow "replace callback function with matches" to know the function (match) There are other parameters (see developer.mozilla.org).
Then, the problem is solved by changing to the following code.
Content.replace (/]*src=[']] ([^ ' "]+) [^>]*>/gi, function (match, capture) {
Console.log (Capture);
});
Output results:
Http://static.cnblogs.com/images/logo_small.gif
Get!
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/