Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript to replace Chinese characters

I am building a JavaScript array depending on the input of the user. The array is building fine but if the user enters Chinese symbols it crashes. I'm assuming that it is if the user enters a chinese " or a , or a '. I have the program replacing the English versions of this but i don't know how to replace the Chinese versions of it.

Can anyone help?

Thanks to all for their input

like image 974
Wesley Skeen Avatar asked Oct 25 '11 10:10

Wesley Skeen


1 Answers

From What's the complete range for Chinese characters in Unicode?, the CJK unicode ranges are:

  • 4E00-9FFF (common)
  • 3400-4DFF (rare)
  • F900-FAFF (compatability - Duplicates, unifiable variants, corporate characters)
  • 20000-2A6DF (rare, historic)
  • 2F800-2FA1F (compatability - supplement)

Because JS strings only support UCS-2, which max out at FFFF, the last two ranges probably aren't of great interest. Thus, if you're building a JS string should be able to filter out chinese characters using something like:

replace(/[\u4e00-\u9fff\u3400-\u4dff\uf900-\ufaff]/g, '')
like image 60
broofa Avatar answered Sep 20 '22 01:09

broofa