使用jquery / javascript在Client End上格式化html数据

时间:2014-06-11 10:41:44

标签: javascript jquery html regex

我需要一个正则表达式来处理在文本编辑器中输入的文本的格式。

请参阅此html

<p class="MsoNormal" style="margin-bottom:0cm;margin-bottom:.0001pt;line-height:
normal;mso-pagination:none;mso-layout-grid-align:none;text-autospace:none;text-align:center;"><b><span style="font-   size:14.0pt;font-family:&quot;Times-Roman&quot;,&quot;serif&quot;;mso-bidi-font-family:
 Times-Roman;color:#1B1E20">-- Michael Hirst</span></b><span style="font-size:
14.0pt;font-family:&quot;Times-Roman&quot;,&quot;serif&quot;;mso-bidi-font-family:Times-Roman;
color:#1B1E20">, creator, writer, and executive producer,&nbsp;Vikings</span><o:p></o:p></p> 

预期产出:

<p style="text-align:center;"><span>--Michael Hirst</span></b><span>creator, writer, and executive producer,Vikings</span>

我需要从html中删除类属性。对于内联样式,我需要除了text-align之外。 格式化之后我需要除了少量标签之外,就像我们在php中使用strip_tags

一样
 strip_tags($html,"<b><h2><h3><h4><strong><div><p><br><ul><li><ol><blockquote>")

这样做的正则表达式是什么。我是这个正则表达式的新手!!

1 个答案:

答案 0 :(得分:0)

请勿尝试使用正则表达式解析html。搜索SO - 有很多例子说明为什么不这样做。正如您使用jquery标记了您的问题,您可以使用类似的东西作为起点:

var $html = $('.MsoNormal + span').html();
$('p').css('text-align','center').append($html);

DEMO here