富文本编辑器,去除粘贴的word文件中的无效代码

编程入门 行业动态 更新时间:2024-10-10 02:19:50

富文本<a href=https://www.elefans.com/category/jswz/34/1769874.html style=编辑器,去除粘贴的word文件中的无效代码"/>

富文本编辑器,去除粘贴的word文件中的无效代码

将word文件中的内容粘贴到富文本编辑器中时,会发现存在很多无效的代码,格式类似下图:

<!--[if gte mso 9]><xml><o:OfficeDocumentSettings><o:RelyOnVML/><o:AllowPNG/></o:OfficeDocumentSettings>
</xml><![endif]-->
<!--[if gte mso 9]><xml><w:WordDocument><w:View>Normal</w:View><w:Zoom>0</w:Zoom><w:TrackMoves/><w:TrackFormatting/><w:PunctuationKerning/><w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:LsdException Locked="false" Priority="37" Name="Bibliography"/><w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/></w:LatentStyles>
</xml><![endif]-->

那么,删除无效代码可以使用下面的方法:

function cleanWordHtml(text) {text= text.replace(/<!--\[if gte mso [0-9]{1,2}\]>[\s\S]*?<!\[endif\]-->/ig, '')text= text.replace(/<!--*?-->/ig, '')text= text.replace(/<style>[\s\S]*?mso[\s\S]*?<\/style>/ig, '')text= text.replace(/ lang=".+?"/ig, '')text= text.replace(/<o:p><\/o:p>/ig, '')text= text.replace(/ class="Mso.+?"/ig, '')text= text.replace(/ mso-spacerun: 'yes'/ig, '')return text
}

这样保存到数据库的内容就不会存在无效代码。 

更多推荐

富文本编辑器,去除粘贴的word文件中的无效代码

本文发布于:2024-02-13 14:58:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759092.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑器   文本   代码   文件   word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!