问题描述
我的应用程序依赖此函数来测试字符串是否为韩语:
My application was relying on this function to test if a string is Korean or not :
const isKoreanWord = (input) => {
const match = input.match(/[\u3131-\uD79D]/g);
return match ? match.length === input.length : false;
}
isKoreanWord('만두'); // true
isKoreanWord('mandu'); // false
直到我开始包含中文支持,现在这个功能不连贯:
until I started to include Chinese support and now this function is incoherent :
isKoreanWord('幹嘛'); // true
我认为这是因为韩国人物和中国人混杂在一起相同的Unicode范围。
I believe this is caused by the fact that Korean characters and Chinese ones are intermingled into the same Unicode range.
如果输入只包含韩文字符,我应如何更正此函数使其返回 true
?
How should I correct this function to make it returns true
if the input contains only Korean characters ?
推荐答案
这是Hangul所需的unicode范围(取自他们的页面)。
Here is the unicode range you need for Hangul (Taken from their wikipedia page).
U+AC00–U+D7AF
U+1100–U+11FF
U+3130–U+318F
U+A960–U+A97F
U+D7B0–U+D7FF
所以你的正则表达式 .match
应如下所示:
So your regex .match
should look like this:
const match = input.match(/[\uac00-\ud7af]|[\u1100-\u11ff]|[\u3130-\u318f]|[\ua960-\ua97f]|[\ud7b0-\ud7ff]/g);
这篇关于使用JavaScript测试输入是韩语还是中文的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!