Convert Microsoft Word Characters
March 31, 2009 at 5:18 am Taco Fleur
I’ve written a JavaScript function a while ago that converts Microsoft Word Characters (crap) to valid HTML characters. Till today I didn’t realize how valuable the function is, it could probably do with some expansion on what it converts so any help to improve is much appreciated.
For those needing the function, just send me an email or leave a comment.
/*
‘ = 8216 convert to '
’ = 8217 convert to '
“ = 8220 convert to "
” = 8221 convert to "
bullet points from word: 61558, 8226, 61607
convert them all to 111
dash 8211 convert to -
... 8230 convert to ...
trademark symbol 8482 convert to TM
copyright symbol 169 convert to (c)
registered symbol 174 convert to (r)
>> 187 convert to >>
<< 171 convert to <<
function convertCharacter( obj ) {
var myRegEx = new RegExp( String.fromCharCode( 8216 )
+ "|" + String.fromCharCode( 8217 ), "g" );
obj.value = obj.value.replace( myRegEx, "'" );
var myRegEx = new RegExp( String.fromCharCode( 8220 )
+ "|" + String.fromCharCode( 8221 ), "g" );
obj.value = obj.value.replace( myRegEx, '"' );
var myRegEx = new RegExp( String.fromCharCode( 61558 )
+ "|" + String.fromCharCode( 8226 ) + "|"
+ String.fromCharCode( 61607 ), "g" );
obj.value =
obj.value.replace( myRegEx, String.fromCharCode( 111 ) );
var myRegEx = new RegExp( String.fromCharCode( 8211 ), "g" );
obj.value = obj.value.replace( myRegEx, "-" );
var myRegEx = new RegExp( String.fromCharCode( 8230 ), "g" );
obj.value = obj.value.replace( myRegEx, "..." );
var myRegEx = new RegExp( String.fromCharCode( 8482 ), "g" );
obj.value = obj.value.replace( myRegEx, "TM" );
var myRegEx = new RegExp( String.fromCharCode( 169 ), "g" );
obj.value = obj.value.replace( myRegEx, "(c)" );
var myRegEx = new RegExp( String.fromCharCode( 174 ), "g" );
obj.value = obj.value.replace( myRegEx, "(r)" );
var myRegEx = new RegExp( String.fromCharCode( 187 ), "g" );
obj.value = obj.value.replace( myRegEx, ">>" );
var myRegEx = new RegExp( String.fromCharCode( 171 ), "g" );
obj.value = obj.value.replace( myRegEx, "<<" );
}
Like this:
Like Loading...
Related
Entry filed under: JavaScript, Programming. Tags: convert word characters.
1.
Charles Smith | April 8, 2009 at 3:04 am
Please send me the function; it would be very helpful. Thanks very much.
LikeLike