I can't get this javascript function to work the way I want...

// matches a String that contains kanji and/or kana character(s)

String.prototype.isKanjiKana = function(){
    return !!this.match(/^[\u4E00-\u9FAF|\u3040-\u3096|\u30A1-\u30FA|\uFF66-\uFF9D|\u31F0-\u31FF]+$/);
}

it does return TRUE if the string is made of kanji and/or kana characters, FALSE if alphabet or other chars are present.

I would like it to return if at least 1 kanji and/or kana characters are present instead that if all of them are.

thank you in advance for any help!

up vote 2 down vote accepted
String.prototype.isKanjiKana = function(){
    return !!this.match(/[\u4E00-\u9FAF\u3040-\u3096\u30A1-\u30FA\uFF66-\uFF9D\u31F0-\u31FF]/);
}

Don't anchor it to beginning and end of string with $^ and the + is useless in this case.

  • thank you! :) I didn't understood the $ in the end and was all focused on the ^ – Mikele Sep 8 '11 at 8:35
  • so much to learn. – Mikele Sep 8 '11 at 8:35

The right answer is not to hardcode ranges. Never ever put magic numbers in your code! That is a maintenance nightmare. It is hard to read, hard to write, hard to debug, hard to maintain. How do you know you got the numbers right? What happens when they add new ones? No, do not use magic numbers. Please.

The right answer is to use named Unicode scripts, which are a fundemental aspect of every Unicode code point:

[\p{Han}\p{Hiragana}\p{Katakana}]

That requires the XRegExp plugin for Javascript.

The real problem is that Javascript regexes on their own are too primitive to support Unicode properties — and therefore, to support Unicode. Maybe that was once an acceptable compromise 15 years ago, but today it is nothing less than intolerably negligent, as you yourself have discovered.

You will also miss a few Common code points specified as kana in the new Script Extensions property, but probably no matter. You could just add \p{Common} to the set above.

  • 2
    So he should trust XRegExp (that will do exactly the same, hardcode numbers)? Mmmh... The last release is from 2010-03-24, the last blog entry is 2010-07-05... No, he did the right thing: he created a method with the right name and extended string. Adding the reference to another library IS normally a nightmare ready to raise. – xanatos Sep 8 '11 at 20:13
  • Sorry, but tchrist is right. It's true that eventually, somewhere, somehow, there will be database of codepoint numbers, that's inevitable, but unless your specialty is maintaining databases of unicode propertied, you don't want to write your own functions. Even if you did, the number ranges should at a minimum come from a config file. Hard-coding in number-ranges will only lead to bugs down the road. New versions of the unicode databases are released periodically with changes, eratta, etc. (Also, note that something that works 95%, that you trust is worse than nothing in many cases). – Noah Aug 19 '12 at 7:36
  • XRegExp's categories (like \p{Han} et al.) compile down to regular JavaScript regexps. Here are the native Javascript regexp for Han: /[\u2E80-\u2E99\u2E9B-\u2EF3\u2F00-\u2FD5\u3005\u3007\u3021-\u3029\u3038-\u303B\u3400-\u4DB5\u4E00-\u9FCC\uF900-\uFA6D\uFA70-\uFAD9]/. For Katakana: /[\u30A1-\u30FA\u30FD-\u30FF\u31F0-\u31FF\u32D0-\u32FE\u3300-\u3357\uFF66-\uFF6F\uFF71-\uFF9D]/. And for Hiragana: /[\u3041-\u3096\u309D-\u309F]/. Forgive me for not including \p{Common} here as it's too big for this commentbox. – Ahmed Fasih Aug 15 '14 at 18:17
  • XRegExp’s Han category: XRegExp('\\p{Han}') is now simplified: /[⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〻㐀-䶵一-鿕豈-舘並-龎]/ – Ahmed Fasih Nov 19 '16 at 2:35
/[\u4E00-\u9FAF|\u3040-\u3096|\u30A1-\u30FA|\uFF66-\uFF9D|\u31F0-\u31FF]/
  • Excuse me :-( Wrong paste :-( I have rollbacked and I hope everything is ok. – xanatos Sep 8 '11 at 8:09
  • @xanatos No problemo :) – Paulpro Sep 8 '11 at 8:21
  • fantastic! thank you! :) – Mikele Sep 8 '11 at 8:34
  • 3
    @PaulPRO:This should also accept pipe character | – Toto Sep 8 '11 at 8:48

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.