Convert Microsoft Word Characters

March 31, 2009 at 5:18 am 1 comment


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, "<<" ); }
Advertisements

Entry filed under: JavaScript, Programming. Tags: .

Why use JavaScript Form Field Validation? Facebook advertising

1 Comment Add your own

  • 1. Charles Smith  |  April 8, 2009 at 3:04 am

    Please send me the function; it would be very helpful. Thanks very much.

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 19 other followers

Archives

Top Rated

Top Clicks

  • None

Blog Stats

  • 168,382 hits

%d bloggers like this: