javascript - 使用 jquery/javascript 在客户端格式化 html 数据

标签 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 中删除类属性。对于内联样式,我只需要文本对齐除外。 格式化后,我需要像我们在 php 中使用 strip_tags 那样除去一些标签

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

执行此操作的正则表达式是什么。我是这个正则表达式的新手!

最佳答案

不要尝试使用正则表达式解析 html。搜索一下 SO - 有很多例子可以说明为什么不这样做。当您使用 jquery 标记您的问题时,您可以使用类似这样的内容作为起点:

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

DEMO here

关于javascript - 使用 jquery/javascript 在客户端格式化 html 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24160857/

相关文章:

javascript - 使用 jQuery 要求用户按下键盘上的每个字母和数字

javascript - 尝试使用 Chart.js 设置刻度刻度 beginAtZero 不成功,为什么?

javascript - 有没有办法使用 input type= time 禁用 future 时间

Javascript 日期字符串格式

javascript - jQuery:如何将 div 内的串联元素与 div 的其余部分分开设置样式?

javascript - 将 javascript 变量值传递给输入类型隐藏值

javascript - 我的应用程序框架的 js/jquery 代码有问题

javascript - 打开时的放大弹出焦点按钮

javascript - CSS3 Flex 布局右侧留空

html - wordpress重定向到主页和seo