Javascript Replace Unicode Characters

Hi there,

I’ve been trying to write a JavaScript method that replaces instances of RTF double quotes and single quotes or apostrophes. It seems some of our users are pasting RTF from MS Word and that’s causing some problems in Oracle.

The RTF characters look like this:
“,”, ‘,’
But the unformatted should look like this:
",’

How would you go about implementing replace() using unicode values?

Normally using unformatted text str.replace(‘“’, ‘"’) would work, but that’s not working.

Thanks.

Doug

Unicode characters can be most easily specified in regular expressions (as can be used in the first parameter in replace) using \unnnn where nnnn is the hexadecimal representation of the character in utf-16.

You can find the unicode code point with charCodeAt() method of the string class.
Once you know the number, then you can create the character programatically using String.fromCharCode(theUnicodeCodePoint). Once you have the character, then you can feed it to replace()

You can also just embed the character into a javascript string in the source code like
var theChar = ‘“’;
But, you probably need to make sure you save the file in the proper encoding in your text editor, as well as have your web server serve the proper http header so the browser that downloads it, can understand it.