Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Regex to test for presence of Japanese characters
// REFERENCE UNICODE TABLES:
// http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml
// http://www.tamasoft.co.jp/en/general-info/unicode.html
//
// TEST EDITOR:
// http://www.gethifi.com/tools/regex
//
// UNICODE RANGE : DESCRIPTION
//
// 3000-303F : punctuation
// 3040-309F : hiragana
// 30A0-30FF : katakana
// FF00-FFEF : Full-width roman + half-width katakana
// 4E00-9FAF : Common and uncommon kanji
//
// Non-Japanese punctuation/formatting characters commonly used in Japanese text
// 2605-2606 : Stars
// 2190-2195 : Arrows
// u203B : Weird asterisk thing
var regex = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/g;
var input = "input string";
if(regex.test(input)) {
console.log("Japanese characters found")
}
else {
console.log("No Japanese characters");
}
@ryanmcgrath

This comment has been minimized.

Copy link
Owner Author

@ryanmcgrath ryanmcgrath commented May 20, 2011

This is a kickass bit of code right here.

@pinopino

This comment has been minimized.

Copy link

@pinopino pinopino commented Jan 15, 2014

it works! thank you for your amazing work!

@swathiPaipalle

This comment has been minimized.

Copy link

@swathiPaipalle swathiPaipalle commented Jan 24, 2014

Thanks! It helps me

@FlashJunior

This comment has been minimized.

Copy link

@FlashJunior FlashJunior commented Nov 17, 2014

thx!

@koreahadif

This comment has been minimized.

Copy link

@koreahadif koreahadif commented Dec 4, 2014

Thanks~!! It works

@ram4git

This comment has been minimized.

Copy link

@ram4git ram4git commented Apr 15, 2017

Why can't the initial few or conditions be consolidated into [\u3000-\u30FF]?

@den-chan

This comment has been minimized.

Copy link

@den-chan den-chan commented Apr 30, 2017

As a side note, the characters from 0x4e00 to 0x9faf include Chinese-only characters. The following code will give you a list of the standard 6355 Japanese kanji:

for (var i = 0x4e00, acc=[]; i < 0x9faf; i++) acc.push(String.fromCharCode(i));
var sortedChars = acc.sort(Intl.Collator("ja-JP").compare);
var level1Kanji = sortedChars.slice(0, 2965); // JIS X 0208 - Level 1 Kanji (2965 characters)
var level2Kanji = sortedChars.slice(2965, 6355) // JIS X 0208 - Level 2 Kanji (3390 characters)
@mortress

This comment has been minimized.

Copy link

@mortress mortress commented Jun 30, 2017

Thanks! It helps me

@hanhpp

This comment has been minimized.

Copy link

@hanhpp hanhpp commented Aug 22, 2017

Thanks, this helped me too.

@AndrewThian

This comment has been minimized.

Copy link

@AndrewThian AndrewThian commented Nov 17, 2017

my god you lifesaver <3 hearts and cookies!

@paulgaumer

This comment has been minimized.

Copy link

@paulgaumer paulgaumer commented Dec 4, 2017

Big thank you, this helped a lot!

@blcsntb

This comment has been minimized.

Copy link

@blcsntb blcsntb commented May 22, 2018

Thank you!

@wlgnsdh

This comment has been minimized.

Copy link

@wlgnsdh wlgnsdh commented Aug 24, 2018

thank you!!

@qinatsu

This comment has been minimized.

Copy link

@qinatsu qinatsu commented Oct 7, 2018

thx

@Yatufo

This comment has been minimized.

Copy link

@Yatufo Yatufo commented Nov 7, 2018

thanks!

@sudheeshms

This comment has been minimized.

Copy link

@sudheeshms sudheeshms commented Jan 24, 2019

(y)

@KeshavGeek

This comment has been minimized.

Copy link

@KeshavGeek KeshavGeek commented Jul 15, 2019

Thanks for such creative work.
Cheers

@wtberry

This comment has been minimized.

Copy link

@wtberry wtberry commented Sep 5, 2019

Thanks, super useful code!

@icarcal

This comment has been minimized.

Copy link

@icarcal icarcal commented Mar 20, 2020

This is awesome 👏 🎉 🚀

@hoandm

This comment has been minimized.

Copy link

@hoandm hoandm commented Jul 14, 2020

THANKS (Y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.