jquery从文本区域中删除MS word格式

问题描述:

我想从我的文本区域中删除MSWord格式信息,但没有弄清楚如何执行此操作。 这种情况就像我需要将MSWord中的一些内容粘贴到文本框编辑器中一样。 它被复制得很好,但问题是所有格式也被复制,所以我的300个字符的句子扩展为20000个字符格式的句子。 任何人都可以告诉我该怎么做?jquery从文本区域中删除MS word格式

好吧有些R & D我已经达到了一定的阶段。

下面是我从Word文档复制的文本

Once the user clicks on the Cancel icon for a transaction on the Status of Business, and the transaction is eligible for cancellation, a new screen titled “Cancel Transaction” will appear, with the following fields: 

这里就是我得到了$( “#textAreaId”)。VAL()

" 

    Normal 
    0 




    false 
    false 
    false 

    EN-US 
    X-NONE 
    X-NONE 




























Once the user clicks on the Cancel icon for a 
transaction on the Status of Business, and the transaction is eligible for 
cancellation, a new screen titled “Cancel Transaction” will appear, with the 
following fields: 



/* Style Definitions */ 
table.MsoNormalTable 
    {mso-style-name:"Table Normal"; 
    mso-style-parent:""; 
    line-height:115%; 
    font-:11.0pt;"Calibri","sans-serif"; 
    mso-bidi-"Times New Roman";} 

" 
+0

你可以添加应显示的文本请 – skyfoot 2013-05-07 11:09:34

+0

文本可能是一个实际上,我在上面的示例中放置的文本只是格式化...而且它非常庞大..所以我只是把一块大块放在那里。我需要显示的真实文本是沿着页面 – Gautam 2013-05-07 11:10:40

+0

我想帮助你,但我不想破译你给出的例子来看看应该显示什么。我想看看哪些字符需要删除 – skyfoot 2013-05-07 11:13:25

我终于找到了解决办法 这里是它

// removes MS Office generated guff 
function cleanHTML(input) { 
    // 1. remove line breaks/Mso classes 
    var stringStripper = /(\n|\r| class=(")?Mso[a-zA-Z]+(")?)/g; 
    var output = input.replace(stringStripper, ' '); 
    // 2. strip Word generated HTML comments 
    var commentSripper = new RegExp('<!--(.*?)-->','g'); 
    var output = output.replace(commentSripper, ''); 
    var tagStripper = new RegExp('<(/)*(meta|link|span|\\?xml:|st1:|o:|font)(.*?)>','gi'); 
    // 3. remove tags leave content if any 
    output = output.replace(tagStripper, ''); 
    // 4. Remove everything in between and including tags '<style(.)style(.)>' 
    var badTags = ['style', 'script','applet','embed','noframes','noscript']; 

    for (var i=0; i< badTags.length; i++) { 
    tagStripper = new RegExp('<'+badTags[i]+'.*?'+badTags[i]+'(.*?)>', 'gi'); 
    output = output.replace(tagStripper, ''); 
    } 
    // 5. remove attributes ' style="..."' 
    var badAttributes = ['style', 'start']; 
    for (var i=0; i< badAttributes.length; i++) { 
    var attributeStripper = new RegExp(' ' + badAttributes[i] + '="(.*?)"','gi'); 
    output = output.replace(attributeStripper, ''); 
    } 
    return output; 
} 
+0

很棒的功能! – 2016-10-25 12:03:15