模式匹配可以这样写:import console;
var str = `
asdasds<img 3443 Src='http://xxx.com/1.jpg11' >asdasdasda看了sds
<img b src="http://xxx.com/1.jpg22" >asdasd
<img b src=http://xxx.com/1.jpg333 >asdasd
`;
for img in string.gmatch( str,"<@@<img@>[^>]+") {
var src = string.match(img,`<@@src@>\s*=\s*(<'[^']+'>|<"[^"]+">|<\S+>)`);
console.log(src)
}
console.pause(true);
也可以用新版的 string.html 解析,
import string.html;
import console;
var html = `
asdasds<img 3443 Src='http://xxx.com/1.jpg11' >asdasdasda看了sds
<img b src="http://xxx.com/1.jpg22" >asdasd
<img b SRC=http://xxx.com/1.jpg333 >asdasd
`;
//解析HTML
var htmlDoc = string.html( html)
//获取所有img节点
var imgs = htmlDoc.queryEles( tagName = "img");
//遍历img
for i,img in table.eachIndex(imgs){
console.log( img.getAttribute("src") )
}
console.pause()
我刚加的 getAttribute 函数, 可以支持忽略大小写查找src,Src,SRC....这种属性,不过一般应当没人会把HTML写成这样。
|