Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第21天 写一个方法验证是否为中文 #72

Open
haizhilin2013 opened this issue May 6, 2019 · 13 comments
Open

[js] 第21天 写一个方法验证是否为中文 #72

haizhilin2013 opened this issue May 6, 2019 · 13 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第21天 写一个方法验证是否为中文

@haizhilin2013 haizhilin2013 added the js JavaScript label May 6, 2019
@mlgzzz
Copy link

mlgzzz commented May 7, 2019

function isChinese(str) {
  const re = /^[\u4e00-\u9fa5]+$/;
  return re.test(str);
}

@haizhilin2013
Copy link
Collaborator Author

@mlgzzz 能解释下正则的意思吗?尤其是\u4e00-\u9fa5,从何得来的?

@mlgzzz
Copy link

mlgzzz commented May 7, 2019

@mlgzzz 能解释下正则的意思吗?尤其是\u4e00-\u9fa5,从何得来的?

使用的Unicode 编码 4e00 9fa5 分别表示第一个汉字和最后一个汉字的编码

@myprelude
Copy link

function isChinese(str) {
  return /^[\u4e00-\u9fa5]+$/.test(str);
}

@Konata9
Copy link

Konata9 commented Aug 5, 2019

由于中文比较特殊,最稳妥的还是使用 unicode 来进行匹配。这两个 unicode 分别表示第一个和最后一个汉字。

const isChinese = str => /^[\u4e00-\u9fa5]+$/.test(str);

isChinese("绊爱初号机最高");
isChinese("我完全爱上Warma了");

如果只是很粗暴的需要匹配汉字的话,可以使用 \W,这样也能摘出汉字。不过会混杂逗号、句号等符号。

const isRawChinese = str => /^\W+$/.test(str);

isRawChinese("绊爱初号机最高");
isRawChinese("我完全爱上Warma了");

@15190408121
Copy link

function chinese(str) {
return /^[\u4e00-\u9fa5]+$/.test(str);
}
chinese("我完全爱上Warma了");

@yunlzhang
Copy link

按照中文字符对应编写正则

字符集 字数 Unicode 编码
基本汉字 20902字 4E00-9FA5
基本汉字补充 38字 9FA6-9FCB
扩展A 6582字 3400-4DB5
扩展B 42711字 20000-2A6D6
扩展C 4149字 2A700-2B734
扩展D 222字 2B740-2B81D
康熙部首 214字 2F00-2FD5
部首扩展 115字 2E80-2EF3
兼容汉字 477字 F900-FAD9
兼容扩展 542字 2F800-2FA1D
PUA(GBK)部件 81字 E815-E86F
部件扩展 452字 E400-E5E8
PUA增补 207字 E600-E6CF
汉字笔画 36字 31C0-31E3
汉字结构 12字 2FF0-2FFB
汉语注音 22字 3105-3120
注音扩展 22字 31A0-31BA
1字 3007
/[\u4E00-\u9FFF\u3400-\u4DBF\uF900-\uFAFF\U00020000-U0002EBEF]/

@MrZ2019
Copy link

MrZ2019 commented Sep 21, 2020

function isChinese(str) {
const re = /^[\u4e00-\u9fa5]+$/;
return re.test(str);
}

@378406712
Copy link

function reg(str){
const regExp =/[\u4e00-\u9fa5]/g
return regExp.test(str)
}

@amikly
Copy link

amikly commented Nov 9, 2021

// 是否全为中文
function isChinese(str) {
    // /^[\u4E00-\u9FA5]+$/
    let reg = /[^\u4e00-\u9fa5]/;
    if (reg.test(str)) {
        return false;
    }
    return true;
}

// 是否含有中文
function isHasChinese(str) {
    let reg = /.*[\u4e00-\u9fa5]+.*$/;
    if (reg.test(str)) {
        return false;
    }
    return true;
}

@github-cxtan
Copy link

function CheckIsChinese(data) {
let ret = true;
for (let index = 0; index < data.length; index++) {
if (data.charCodeAt(index) < 0x4e00 || data.charCodeAt(index) > 0x9fa5) {
ret = false;
break;
}
}
return ret;
}

function CheckIsAllChinese(data) {
let reg = /[^\u4e00-\u9fa5]/g;
return !reg.test(data)
}

@yxllovewq
Copy link

const isCN = str => /^[\u4e00-\u9fa5]+$/.test(str);

@xiaoqiangz
Copy link

// 判断字符是否是中文
var str6 = '我是n是啊nasd我'
function isCn(str) {
let data = str6.split('')
return data.map((v, i) => {
return v.charCodeAt() > 255
})

  let reg = /[^\u4e00-\u9fa5]/g;
  return reg.test(data)
}
console.log(isCn(str6))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests