How do I check if a variable contains Chinese or Japanese characters? I know that this line works:
if (document.body.innerText.match(/[\u3400-\u9FBF]/))
I need to do the same thing not for the document but for a single variable.
The Chinese language (at the risk of stating the obvious) is a very complex language, but a simple way to identify Chinese characters is that they are square and not curvy. Japanese characters look rounder and more curvy. Visually, both Japanese and Korean are also more open and spacious than Chinese, which is denser.
Optical character recognition (OCR) – Many apps and websites provide OCR features where you can scan or take pictures of the character(s) you want to look up. Google Docs has such a feature and there are others online you can easily find by searching for “Chinese” and “OCR”.
If the writing you're looking at is made up solely of kanji without any other types of characters, it is most likely Chinese. If you see other characters that look different, such as Japanese hiragana, you're more likely reading Japanese.
If you have a string containing all the Japanese characters, then you can use wstring::find_first_of (). If npos is returned, it means that none of the characters is in the given string. I still don't know how to do what I want to do.
There are numerous ways to check if a string contains Chinese characters. But for the sake of simplicity, we will use the regular expression and ternary operator (?) to accomplish our goal. The test () method of RegExpObject is used to perform a pattern search in a string and returns a Boolean value.
Change RegEx to allow for both English & Japanese characters 1 Regex in JavaScript to match writing systems without word boundaries Related 3457 How can I check for an empty/undefined/null string in JavaScript?
Chinese, Japanese and Korean languages require a fixed width sequence of two bytes for every character, which allows for about 65,000 characters.
.match
is a string method. You can apply it to anything that contains string. And, of course, to arbitrary variable.
In case you have something that is not string, most objects define .toString()
method that converts its content to some reasonable stringified form. When you retrieve selection from page, you get selection object. Convert it to string and then use match
on it: sel.toString().match(...)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With