使用JavaScript清除Microsoft Word粘贴文本 [英] Clean Microsoft Word Pasted Text using JavaScript

查看:147
本文介绍了使用JavaScript清除Microsoft Word粘贴文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用'contenteditable'< div /> 并启用PASTE。

I am using a 'contenteditable' <div/> and enabling PASTE.

它是令人惊讶的是从Microsoft Word的剪贴板副本粘贴的标记代码量。我正在与此作斗争,并且已经使用Prototypes的 stripTags()函数()获得大约1/2的方式,但遗憾的是,这似乎不能让我保留一些标签)。

It is amazing the amount of markup code that gets pasted in from a clipboard copy from Microsoft Word. I am battling this, and have gotten about 1/2 way there using Prototypes' stripTags() function (which unfortunately does not seem to enable me to keep some tags).

然而,即使在那之后,我最终还是会花费大量不必要的标记代码。

However, even after that, I wind up with a mind-blowing amount of unneeded markup code.

所以我的问题是,是否有一些函数(使用JavaScript),或者我可以使用哪种方法来清理大部分不需要的标记?

So my question is, is there some function (using JavaScript), or approach I can use that will clean up the majority of this unneeded markup?

推荐答案

这是我编写的函数,它能很好地完成工作(据我所知,无论如何)。

Here is the function I wound up writing that does the job fairly well (as far as I can tell anyway).

如果有人有任何建议,我当然愿意接受改进建议。谢谢。

I am certainly open for improvement suggestions if anyone has any. Thanks.

function cleanWordPaste( in_word_text ) {
 var tmp = document.createElement("DIV");
 tmp.innerHTML = in_word_text;
 var newString = tmp.textContent||tmp.innerText;
 // this next piece converts line breaks into break tags
 // and removes the seemingly endless crap code
 newString  = newString.replace(/\n\n/g, "<br />").replace(/.*<!--.*-->/g,"");
 // this next piece removes any break tags (up to 10) at beginning
 for ( i=0; i<10; i++ ) {
  if ( newString.substr(0,6)=="<br />" ) { 
   newString = newString.replace("<br />", ""); 
  }
 }
 return newString;
}

希望这对你们中的一些人有所帮助。

Hope this is helpful to some of you.

这篇关于使用JavaScript清除Microsoft Word粘贴文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆