我从后端得到style
。 它有不需要的前缀。 我会替换没有前缀的相同内容。 正确的方法是什么?
这是我得到的:
<style>
043BF83A8FB24A418DA4248840101DE5 .cls_0 {
font:26px 'Arial';
color:rgb(0,0,0);
font-weight:bold;
}
043BF83A8FB24A418DA4248840101DE5 .cls_1 {
font:26px 'Arial';
color:rgb(0,0,0);
}
043BF83A8FB24A418DA4248840101DE5 .cls_11 {
font:13px 'Arial';
color:rgb(0,0,0);
}
043BF83A8FB24A418DA4248840101DE5 .cls_12 {
font:16px 'Arial';
color:rgb(0,0,0);
font-weight:bold;
}
043BF83A8FB24A418DA4248840101DE5 .cls_13 {
font:16px 'Arial';
color:rgb(0,0,0);
}
</style>
这是我的要求。
<style>
.cls_0 {
font:26px 'Arial';
color:rgb(0,0,0);
font-weight:bold;
}
.cls_1 {
font:26px 'Arial';
color:rgb(0,0,0);
}
.cls_11 {
font:13px 'Arial';
color:rgb(0,0,0);
}
.cls_12 {
font:16px 'Arial';
color:rgb(0,0,0);
font-weight:bold;
}
.cls_13 {
font:16px 'Arial';
color:rgb(0,0,0);
}
</style>
我的尝试,但失败了:
var styleText = $('style').get(1).html();
console.log(styleText); //throw error as "Uncaught TypeError: undefined is not a function"
这是一个解决方案:
$('style').html(function(_,h){
return h.replace(/^w+ (.w+)/gm,'$1');
});
这将删除任何<style>
元素中行首和类选择器之前的任何字符串。
示范
前缀似乎是十六进制的,所以这将完成这项工作:
str.replace(/^[A-F0-9]+s+/gm, '')