<img src="image22.jpg" data-a="a2a"><p>Some text with no image</p><img src="i23mage3..."/>

js正则搜索img标签并替换src值

61次阅读
没有评论
const html = '<img src="image1.jpg" data-a="aa"><img src="image22.jpg" data-a="a2a"><p>Some text with no image</p><img src="i23mage3.jpg"  1  >';
 
const regex = /(<img.*?src=")(.*?)(".*?>)/g; //用于匹配<img>标签中的src属性的正则表达式
const matches = html.match(regex); //使用match()方法获取匹配结果
 
 
 
var sources = matches.map(match => match.replace(regex, '$2')); //$序号从1开始,$1为第一个分组
var sources = matches.map(match => match.replace(regex, '$1$2'+'好$3')); //
 
 
console.log(sources)
正文完
 
wujingquan
版权声明:本站原创文章,由 wujingquan 2023-12-05发表,共计408字。
转载说明:Unless otherwise specified, all articles are published by cc-4.0 protocol. Please indicate the source of reprint.
评论(没有评论)