js 如何匹配span style=colorred
可以使用正则表达式来匹配 <span style="color:red">。
以下是一个示例代码:
var str = '<span style="color:red">Hello World</span>';
var regex = /<span style="color:red">([^<]+)<\/span>/;
var match = regex.exec(str);
if (match) {
var content = match[1];
console.log(content); // 输出 "Hello World"
} else {
console.log("未匹配到结果");
}
这里的正则表达式 /<span style="color:red">([^<]+)<\/span>/ 匹配 <span style="color:red"> 开始的字符串,并获取 ([^<]+) 中间的内容作为匹配结果。[^<]+ 表示匹配除了 < 之外的任意字符,<\/span> 表示匹配 </span> 结尾的字符串。匹配结果将保存在 match 数组中的第一个元素 match[1] 中
原文地址: http://www.cveoy.top/t/topic/iZP0 著作权归作者所有。请勿转载和采集!