开发者

Detecting a unicode character (kanji) from a range of characters in Javascript?

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 us开发者_JAVA技巧ing Javascript to test over this range?


/[\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
}


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 [一-龯]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜