3

I am trying to read input and pass it only if it is a Japanese kanji. That means the character falls somewhere between 4e00 and 9faf, according to http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml

Is it possible using Javascript to test over this range?

Makoto
  • 104,088
  • 27
  • 192
  • 230
ssb
  • 1,352
  • 3
  • 18
  • 40

2 Answers2

3

/[\u4e00-\u9faf]+/ should do it. That matches one or more characters in the range 4e00 - 9faf.

From the the MDN documentation:

\uhhhh Matches the character with the code hhhh (four hexadecimal digits).

So in your case, if you want to test the whole string, you probably want:

if(/^[\u4e00-\u9faf]+$/.test(str)) {
    // str is a kanji
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • I've tried this exact code but the character I'm testing it with is not passing the test. Specifically 請. – ssb Aug 22 '11 at 17:00
  • `/^[\u4e00-\u9faf]+$/.test("請")` gives me `true`. Maybe you want to expression accept white spaces? – Felix Kling Aug 22 '11 at 17:02
  • My mistake, I was forgetting to add .selectionText back on to the variable. Works now, thank you! – ssb Aug 22 '11 at 17:07
1

I'd just like to add that since asking this question I have learned that it's not necessary to use the raw unicode ID and you can just throw in the character itself. So for example, instead of [\u4e00-\u9faf] you can search over [一-龯]

ssb
  • 1,352
  • 3
  • 18
  • 40