正则表达式提取网页的图片

let html = `<html style="height: 100%;"><head><meta name="viewport" content="width=device-width, minimum-scale=0.1"><title>PCfb_5bf082d29588c07f842ccde3f97243ea.png (540×258)</title><input type="hidden" id="_w_pedant"><script type="text/javascript" src="chrome-extension://dbjbempljhcmhlfpfacalomonjpalpko/scripts/inspector.js"></script></head><body style="margin: 0px; background: #0e0e0e; height: 100%"><img style="-webkit-user-select: none;margin: auto;cursor: zoom-in;background-color: hsl(0, 0%, 90%);transition: background-color 300ms;" src="https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png" width="403" height="192"><img style="-webkit-user-select: none;margin: auto;cursor: zoom-in;background-color: hsl(0, 0%, 90%);transition: background-color 300ms;" src="https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png" width="403" height="192"><img style="-webkit-user-select: none;margin: auto;cursor: zoom-in;background-color: hsl(0, 0%, 90%);transition: background-color 300ms;" src="https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png" width="403" height="192"><img style="-webkit-user-select: none;margin: auto;cursor: zoom-in;background-color: hsl(0, 0%, 90%);transition: background-color 300ms;" src="https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png" width="403" height="192"></body></html>`

/*
	1、提取所有的img标签
	.		匹配除换行符以外的所有字符
	*?	匹配方式是非贪婪式的,找最近
	g		全局匹配
*/ 
let reg = /<img.*?>/g
// 提取出html中所有的img标签
let arr = html.match(reg)
/*
	2、提取所有的img标签中的src值
	\s		匹配一个空格
	src=	匹配src=
	['"]	匹配单引号或双引号 
	(.*?)	非贪婪式匹配非换行符的字符	
*/
let reg1 = /\s+src=['"](.*?)['"]/
let arr1 = arr.map(item=>{
	return item.match(reg1)[1]
})
console.log(arr1)

// 提取的结果arr1
[
  'https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png',
  'https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png',
  'https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png',
  'https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png'
]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值