如何用正则表达式去掉html代码里面的字体和字号

artak 2019-03-26 11:07:56
<span style=\"font-size: 19px;line-height: 150%;font-family: 宋体;\">;aa </span>
如何用正则表达式去掉html代码里面的字体和字号,如上
...全文
447 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
诺维斯基 2019-04-27
  • 打赏
  • 举报
回复
秋的红果实 2019-03-31
  • 打赏
  • 举报
回复

string input = "<span style=\"font-size: 19px;line-height: 150%;font-family: 宋体;\">oooou;aa </span>";
input = Regex.Replace(input, @"font-size:\s*\d+px;|font-family:\s*.+?;", "");

Response.Write("output:"+input);

注意:在web下输出,只能看到oooou;aa ,原因是html标签不显示,右击网页,看html源代码
肖半仙丶1413 2019-03-29
  • 打赏
  • 举报
回复

//清理CSS样式
function CleanWord(oNode) {
var bIgnoreFont = true; //是否忽略font标签
var bRemoveStyles = true; //是否清理CSS样式
var html = oNode;

var r = /style="[^"]*"/g;
var r1 = /style='[^']*'/g;
html = html.replace(r, '');
html = html.replace(r1, '');


html = html.replace(/<o:p>\s*<\/o:p>/g, '');
html = html.replace(/<o:p>[\s\S]*?<\/o:p>/g, ' ');

// Remove mso-xxx styles.
html = html.replace(/\s*mso-[^:]+:[^;"]+;?/gi, '');

// Remove margin styles.
html = html.replace(/\s*MARGIN: 0(?:cm|in) 0(?:cm|in) 0pt\s*;/gi, '');
html = html.replace(/\s*MARGIN: 0(?:cm|in) 0(?:cm|in) 0pt\s*"/gi, "\"");

html = html.replace(/\s*TEXT-INDENT: 0(?:cm|in)\s*;/gi, '');
html = html.replace(/\s*TEXT-INDENT: 0(?:cm|in)\s*"/gi, "\"");

html = html.replace(/\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"");

html = html.replace(/\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"");

html = html.replace(/\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"");

html = html.replace(/\s*tab-stops:[^;"]*;?/gi, '');
html = html.replace(/\s*tab-stops:[^"]*/gi, '');

// Remove FONT face attributes.
if (bIgnoreFont) {
//html = html.replace(/\s*face="[^"]*"/gi, '');
//html = html.replace(/\s*face=[^ >]*/gi, '');
//html = html.replace(/\s*size="[^"]*"/gi, '');
//html = html.replace(/\s*size=[^ >]*/gi, '');
html = html.replace(/<\/?font[^>]*>/gi, "");
html = html.replace(/<\/?FONT[^>]*>/gi, "");

//html = html.replace(/\s*FONT-FAMILY:[^;"]*;?/gi, '');
}
// Remove Class attributes
html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");

// Remove styles.
if (bRemoveStyles) {
html = html.replace(/<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3");
}

// Remove style, meta and link tags
html = html.replace(/<STYLE[^>]*>[\s\S]*?<\/STYLE[^>]*>/gi, '');
html = html.replace(/<(?:META|LINK)[^>]*>\s*/gi, '');

// Remove empty styles.
html = html.replace(/\s*style="\s*"/gi, '');

html = html.replace(/<SPAN\s*[^>]*>\s* \s*<\/SPAN>/gi, ' ');

html = html.replace(/<SPAN\s*[^>]*><\/SPAN>/gi, '');

// Remove Lang attributes
html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");

html = html.replace(/<SPAN\s*>([\s\S]*?)<\/SPAN>/gi, '$1');

html = html.replace(/<FONT\s*>([\s\S]*?)<\/FONT>/gi, '$1');

// Remove XML elements and declarations
html = html.replace(/<\\?\?xml[^>]*>/gi, '');

// Remove w: tags with contents.
html = html.replace(/<w:[^>]*>[\s\S]*?<\/w:[^>]*>/gi, '');

// Remove Tags with XML namespace declarations: <o:p><\/o:p>
html = html.replace(/<\/?\w+:[^>]*>/gi, '');

// Remove comments [SF BUG-1481861].
html = html.replace(/<\!--[\s\S]*?-->/g, '');

html = html.replace(/<(U|I|STRIKE)> <\/\1>/g, ' ');

html = html.replace(/<H\d>\s*<\/H\d>/gi, '');

// Remove "display:none" tags.
html = html.replace(/<(\w+)[^>]*\sstyle="[^"]*DISPLAY\s?:\s?none[\s\S]*?<\/\1>/ig, '');

// Remove language tags
html = html.replace(/<(\w[^>]*) language=([^ |>]*)([^>]*)/gi, "<$1$3");

// Remove onmouseover and onmouseout events (from MS Word comments effect)
html = html.replace(/<(\w[^>]*) onmouseover="([^\"]*)"([^>]*)/gi, "<$1$3");
html = html.replace(/<(\w[^>]*) onmouseout="([^\"]*)"([^>]*)/gi, "<$1$3");

// if (FCKConfig.CleanWordKeepsStructure) {//是否设置直接粘贴为Word
// // The original <Hn> tag send from Word is something like this: <Hn style="margin-top:0px;margin-bottom:0px">
// html = html.replace(/<H(\d)([^>]*)>/gi, '<h$1>');

// // Word likes to insert extra <font> tags, when using MSIE. (Wierd).
// html = html.replace(/<(H\d)><FONT[^>]*>([\s\S]*?)<\/FONT><\/\1>/gi, '<$1>$2<\/$1>');
// html = html.replace(/<(H\d)><EM>([\s\S]*?)<\/EM><\/\1>/gi, '<$1>$2<\/$1>');
// }
// else {
//html = html.replace(/<H1([^>]*)>/gi, '<div$1><b><font size="6">');
//html = html.replace(/<H2([^>]*)>/gi, '<div$1><b><font size="5">');
//html = html.replace(/<H3([^>]*)>/gi, '<div$1><b><font size="4">');
//html = html.replace(/<H4([^>]*)>/gi, '<div$1><b><font size="3">');
//html = html.replace(/<H5([^>]*)>/gi, '<div$1><b><font size="2">');
//html = html.replace(/<H6([^>]*)>/gi, '<div$1><b><font size="1">');
html = html.replace(/<H1([^>]*)>/gi, '<div$1><b><font>');
html = html.replace(/<H2([^>]*)>/gi, '<div$1><b><font>');
html = html.replace(/<H3([^>]*)>/gi, '<div$1><b><font>');
html = html.replace(/<H4([^>]*)>/gi, '<div$1><b><font>');
html = html.replace(/<H5([^>]*)>/gi, '<div$1><b><font>');
html = html.replace(/<H6([^>]*)>/gi, '<div$1><b><font>');

html = html.replace(/<\/H\d>/gi, '<\/font><\/b><\/div>');

// Transform <P> to <DIV>
var re = new RegExp('(<P)([^>]*>[\\s\\S]*?)(<\/P>)', 'gi'); // Different because of a IE 5.0 error
html = html.replace(re, '<div$2<\/div>');

// Remove empty tags (three times, just to be sure).
// This also removes any empty anchor
html = html.replace(/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '');
html = html.replace(/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '');
html = html.replace(/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '');
//}
return html;
}



这个亲测过的
月夜惹人醉 2019-03-27
  • 打赏
  • 举报
回复
var data = $(".product_id");
for (var i = 0; i < data.length; i++) {
var a = document.getElementById(data[i].id).innerHTML;
a = a.replace(/(\n)/g, "");
a = a.replace(/(\t)/g, "");
a = a.replace(/(\r)/g, "");
a = a.replace(/<\/?[^>]*>/g, "");
a = a.replace(/\s*/g, "");
if (a.length > 102) {
document.getElementById(data[i].id).innerHTML = a.substring(0, 102) + "...";
}
else {
document.getElementById(data[i].id).innerHTML = a;
}
}

最近写的一个去处富文本的内容,也是结合网上查到的内容写得,希望对你有帮助
  • 打赏
  • 举报
回复
正则只能匹配简单的几个字母,又不懂语法分析、更不懂语义分析,怎么可能真正搞这类开发?
江湖评谈 2019-03-26
  • 打赏
  • 举报
回复
^font-size\d+\:\s\d+\;

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧