]*>/gi, function (match) { console.log(match);});输出结果是: 代码如下:得">
当前位置:Gxlcms > JavaScript > 用正则表达式替换图片地址img标签_基础知识

用正则表达式替换图片地址img标签_基础知识

时间:2021-07-01 10:21:17 帮助过:63人阅读

开始想到的解决方法是:

代码如下:

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

输出结果是:

代码如下:


得到的是整个img标签,但我期望得到的是src中的网址,这样只需在function(match)中返回新地址就行了。
于是,卡在这里了。。。
后来,通过Google搜索关键字“javascript replace callback”,在stackoverflow中找到了“replace callback function with matches”,才知道function(match)还有其他参数

然后,改为下面的代码,问题就解决了。

代码如下:

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

输出结果:

代码如下:

http://www.gxlcms.com/images/logo.gif

人气教程排行