Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.47 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Source: http://www.1stclassmedia.co.uk/developers/clean-ms-word-formatting.php
  2.  
  3. function CleanWordHTML( str )
  4. {
  5. str = str.replace(/<o:p>\s*<\/o:p>/g, "") ;
  6. str = str.replace(/<o:p>.*?<\/o:p>/g, "&nbsp;") ;
  7. str = str.replace( /\s*mso-[^:]+:[^;"]+;?/gi, "" ) ;
  8. str = str.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, "" ) ;
  9. str = str.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;
  10. str = str.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, "" ) ;
  11. str = str.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;
  12. str = str.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;
  13. str = str.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;
  14. str = str.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;
  15. str = str.replace( /\s*tab-stops:[^;"]*;?/gi, "" ) ;
  16. str = str.replace( /\s*tab-stops:[^"]*/gi, "" ) ;
  17. str = str.replace( /\s*face="[^"]*"/gi, "" ) ;
  18. str = str.replace( /\s*face=[^ >]*/gi, "" ) ;
  19. str = str.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, "" ) ;
  20. str = str.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
  21. str = str.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;
  22. str = str.replace( /\s*style="\s*"/gi, '' ) ;
  23. str = str.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' ) ;
  24. str = str.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;
  25. str = str.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
  26. str = str.replace( /<SPAN\s*>(.*?)<\/SPAN>/gi, '$1' ) ;
  27. str = str.replace( /<FONT\s*>(.*?)<\/FONT>/gi, '$1' ) ;
  28. str = str.replace(/<\\?\?xml[^>]*>/gi, "") ;
  29. str = str.replace(/<\/?\w+:[^>]*>/gi, "") ;
  30. str = str.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;
  31. str = str.replace( /<H1([^>]*)>/gi, '' ) ;
  32. str = str.replace( /<H2([^>]*)>/gi, '' ) ;
  33. str = str.replace( /<H3([^>]*)>/gi, '' ) ;
  34. str = str.replace( /<H4([^>]*)>/gi, '' ) ;
  35. str = str.replace( /<H5([^>]*)>/gi, '' ) ;
  36. str = str.replace( /<H6([^>]*)>/gi, '' ) ;
  37. str = str.replace( /<\/H\d>/gi, '<br>' ) ; //remove this to take out breaks where Heading tags were
  38. str = str.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' ) ;
  39. str = str.replace( /<(B|b)>&nbsp;<\/\b|B>/g, '' ) ;
  40. str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
  41. str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
  42. str = str.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
  43. //some RegEx code for the picky browsers
  44. var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ;
  45. str = str.replace( re, "<div$2</div>" ) ;
  46. var re2 = new RegExp("(<font|<FONT)([^*>]*>.*?)(<\/FONT>|<\/font>)","gi") ;
  47. str = str.replace( re2, "<div$2</div>") ;
  48. str = str.replace( /size|SIZE = ([\d]{1})/g, '' ) ;
  49. return str ;
  50. }