JavaScript to replace Chinese characters

I am creating a JavaScript array depending on user input. The array builds fine, but if the user enters Chinese characters, it crashes. I assume that if the user enters Chinese "either a" or "a". I have a program replacing the English versions of this, but I do not know how to replace the Chinese versions.

Can anyone help?

Thanks to everyone for their input.

+4
source share
4 answers

From What is the full range for Chinese characters in Unicode? Unicode CJK ranges:

  • 4E00-9FFF (general)
  • 3400-4DFF (rare)
  • F900-FAFF (Compatibility - Duplicates, Unified Options, Corporate Symbols).
  • 20000-2A6DF (rare, historical)
  • 2F800-2FA1F (compatibility - addition)

Since JS strings only support UCS-2, the highest of which are in FFFF, the last two ranges are probably not of much interest. Thus, if you create a JS string, you should filter out Chinese characters using something like:

replace(/[\u4e00-\u9fff\u3400-\u4dff\uf900-\ufaff]/g, '') 
+3
source

You need to use unicode replacer. I think this will help you: http://answers.yahoo.com/question/index?qid=20080528045141AAJ0AIS

+2
source

.Net provides a JavaScriptSerializer and a Serialize method that creates properly escaped JavaScript characters (although I personally have not used them with Chinese characters, but there is no reason why it should not work).

+1
source

Based on broofa's answer:

If you just want to find and replace Chinese punctuation as "either" or "a". then you will want to use Unicode characters in the range FF00-FFEF. Here is a PDF from Unicode showing them: http://unicode.org/charts/PDF/UFF00.pdf
I think you should at least replace them: FF01, FF02, FF07, FF0C, FF0E, FF1F and FF61. These should be the main punctuation marks in China. You can use the broofa replacement function.

+1
source

All Articles