26
votes

How can I check whether a given string contains one or more Japanese characters (consisting of kana and/or kanji)?

I saw a similar question here: How can I check if variable contains Chinese/Japanese characters? , and I used the solution to come up with this:

var containsJapanese = string.match(/[\u3400-\u9FBF]/);

However, this gives many false positives.

I've tested it by having a script iterate through the contents of entire web pages-- such as Facebook, Stack Overflow, etc.-- and marking the divs which supposedly contain Japanese text. In those cases, a large number of divs end up getting marked by mistake. I've also tested it on pages that do contain Japanese text, and the Japanese divs there end up getting marked correctly alongside many incorrectly-marked divs.

2
string.match gives string comparison only and not UTF comparisonuser1428716
@user1428716: That is regex being put in there, so it will detect the characters within the range. However, whether it is correct Japanese or not is another matter...nhahtdh

2 Answers

62
votes

Check whether this works or not. I found this website that seems to list all the characters in Unicode that might be used in Japanese text.

The corresponding regex (for single character) would be:

/[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf]/
  -------------_____________-------------_____________-------------_____________
   Punctuation   Hiragana     Katakana    Full-width       CJK      CJK Ext. A
                                            Roman/      (Common &      (Rare)    
                                          Half-width    Uncommon)
                                           Katakana

The ranges are (as quoted from the site):

  • 3000 - 303f: Japanese-style punctuation
  • 3040 - 309f: Hiragana
  • 30a0 - 30ff: Katakana
  • ff00 - ff9f: Full-width Roman characters and half-width Katakana
  • 4e00 - 9faf: CJK unified ideographs - Common and uncommon Kanji
  • 3400 - 4dbf: CJK unified ideographs Extension A - Rare Kanji

I have changed the ranges a bit:

  • I have changed from ff00 - ffef to ff00 - ff9f for Full-width Roman characters and half-width Katakana. The code points from ffa0 - ffdc contains Hangul half-width characters, which is not what you want. You may want to re-add the code points from ffe0 - ffef, but they are mostly half-width punctuations or full-width currency symbols.

You can check the site and take off any range you don't want, or are sure that it will not appear in your input.

1
votes

Use charCode function to detect japanese language. For example, (from website http://www.jpf.go.jp/j/index.html)

 var a=$('a[href$="culture/new/index.html"]').text();
  a=a+'K';
 for(i=0;i<3;i++){ //3 as i knew it was length 3. Please use string.length
    console.log(a.charCodeAt(i));
   //Detect the charCode here and use break on match
 }


 Output : 19968 35239 75