Skip to content

Instantly share code, notes, and snippets.

@ryanmcgrath
Forked from sym3tri/JapaneseRegex.js
Last active March 22, 2025 05:58
Show Gist options
  • Select an option

  • Save ryanmcgrath/982242 to your computer and use it in GitHub Desktop.

Select an option

Save ryanmcgrath/982242 to your computer and use it in GitHub Desktop.
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/;
var input = "input string";
if(regex.test(input)) {
console.log("Japanese characters found")
}
else {
console.log("No Japanese characters");
}
@FlashJunior

Copy link
Copy Markdown

thx!

@koreahadif

Copy link
Copy Markdown

Thanks~!! It works

@ram4git

ram4git commented Apr 15, 2017

Copy link
Copy Markdown

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

@den-chan

den-chan commented Apr 30, 2017

Copy link
Copy Markdown

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

Copy link
Copy Markdown

Thanks! It helps me

@hanhpp

hanhpp commented Aug 22, 2017

Copy link
Copy Markdown

Thanks, this helped me too.

@AndrewThian

AndrewThian commented Nov 17, 2017

Copy link
Copy Markdown

my god you lifesaver <3 hearts and cookies!

@paulgaumer

Copy link
Copy Markdown

Big thank you, this helped a lot!

@binhapp

binhapp commented May 22, 2018

Copy link
Copy Markdown

Thank you!

@wlgnsdh

wlgnsdh commented Aug 24, 2018

Copy link
Copy Markdown

thank you!!

@littletsu

Copy link
Copy Markdown

thx

@Yatufo

Yatufo commented Nov 7, 2018

Copy link
Copy Markdown

thanks!

@sudheeshms

Copy link
Copy Markdown

(y)

@KeshavGeek

Copy link
Copy Markdown

Thanks for such creative work.
Cheers

@wtberry

wtberry commented Sep 5, 2019

Copy link
Copy Markdown

Thanks, super useful code!

@icarcal

icarcal commented Mar 20, 2020

Copy link
Copy Markdown

This is awesome 👏 🎉 🚀

@hoandm

hoandm commented Jul 14, 2020

Copy link
Copy Markdown

THANKS (Y)

@izadiegizabal

Copy link
Copy Markdown

This is perfect, thank you! 🥳

@CescaMerlin

Copy link
Copy Markdown

So helpful, thank you!

@azoom-tran-nhat-anh

Copy link
Copy Markdown

Thanks!!!

@takahirohonda

Copy link
Copy Markdown

OMG 🤯 🤯 🤯 Love it!

@devprantoroy

Copy link
Copy Markdown

Thank you

ghost commented Jan 17, 2023

Copy link
Copy Markdown

Thank you so much, helped me out big time!

@stefan-pribicevic

Copy link
Copy Markdown

Works like a charm, thanks.

@seishunpop

Copy link
Copy Markdown

I believe this code is what the kids call "bussin"

@ChinhNttdata

Copy link
Copy Markdown

Thanks you

@aehlke

aehlke commented Dec 20, 2024

Copy link
Copy Markdown

the g is unnecessary and can cause bugs because it introduces state into the regex that affects subsequent calls to .test

@ryanmcgrath

Copy link
Copy Markdown
Author

@aehlke wow, long time no see! I’ll edit it lol

@aehlke

aehlke commented Dec 20, 2024

Copy link
Copy Markdown

hey @ryanmcgrath it's been a long time indeed! I have this snippet in an older part of my app and hadn't realized it's from you :D hope you're doing well, and thanks for the edit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment