正则
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <img src="" alt="" /> </head> <body> <script> let str = `"<p><img id="1630633989830-0" style="width: 100%; height: auto; display: block;" src="https://wxyl.pension.taikang.com/WX/activity/BGCZone/21/headImg.png" /></p> <p><img id="1630633961028" style="width: 100%; height: auto; display: block;" src="https://wxyl.pension.taikang.com/WX/tkp-activity/2021/09/02/FC74D1F61AB04E5F84C8DDBCE896970F.png" data-poster="http://p5-tkpes-tkgrowth-1305527481.cos.ap-beijing.myqcloud.com/tkpvideo/e9f2f5e6e2b644b483579492ff4c7933.mp4" /><img id="1630634039423-0" style="width: 100%; height: auto; display: block;" src="https://wxyl.pension.taikang.com/WX/activity/BGCZone/21/desc.jpg" />你好</p> <p> </p>"` /* let arr = str.match(/.*src="(.*)"[ ]+data-poster="(.*)"[ ]+\/></) console.log(arr);*/ /* let arr = str.match(/<img\s*.*data-poster\s*="(.*mp4)"\s\/>/) console.log(arr);*/ //匹配 <img 任意字符零次到多次 src="(任意字符)" 空格最少一次 data-poster="(任意字符mp4)" 空格零次或多次 /> let arr = str.match(/<img .* src="(.*)"\s+data-poster="(.*mp4)"\s*\/>/) console.log(arr); /* .* 表示 单个字符匹配任意次数 .*? 是满足条件的情况只匹配一次,即最小匹配. */ </script> </body> </html>