Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if variable contains Chinese/Japanese characters?

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.

like image 869
waivy Avatar asked Jun 26 '12 11:06

waivy


People also ask

How can you tell a Chinese character from a Japanese character?

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.

How do you identify Chinese characters?

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”.

How can you tell the difference between Chinese and Japanese texts?

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.

How do I find all the Japanese characters in a string?

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.

How to check if a string contains Chinese characters in JavaScript?

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.

Is there a regex in JavaScript to match Japanese characters?

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?

How many characters are there in the Chinese Alphabet?

Chinese, Japanese and Korean languages require a fixed width sequence of two bytes for every character, which allows for about 65,000 characters.


1 Answers

.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(...).

like image 169
Oleg V. Volkov Avatar answered Sep 22 '22 21:09

Oleg V. Volkov