var text = '..<anything><anything style="color:red;">hello</anything><anything style="color:blue; font-size:1em;">hello</anything></anything>...';
要么
var text = "..<anything><anything style='color:red;'>hello</anything><anything style='color:blue; font-size:1em;'>hello</anything></anything>...";
结果:
array[0] = "color:red;";
array[1] = "color:blue; font-size:1em;";
最佳答案
如前所述,使用正则表达式解析HTML不是一个好主意。
但是,如果您确定要这样做,这将为您完成工作:
var matches = text.match(/\bstyle=(['"])(.*?)\1/gi);
var styles = [];
for(var i = 0; i < matches.length; i++) {
styles.push(matches[i].substring(7, matches[i].length - 1));
}