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
Comments
function isChinese(str) {
const re = /^[\u4e00-\u9fa5]+$/;
return re.test(str);
} |
@mlgzzz 能解释下正则的意思吗?尤其是\u4e00-\u9fa5,从何得来的? |
使用的 |
function isChinese(str) {
return /^[\u4e00-\u9fa5]+$/.test(str);
} |
由于中文比较特殊,最稳妥的还是使用 const isChinese = str => /^[\u4e00-\u9fa5]+$/.test(str);
isChinese("绊爱初号机最高");
isChinese("我完全爱上Warma了"); 如果只是很粗暴的需要匹配汉字的话,可以使用 const isRawChinese = str => /^\W+$/.test(str);
isRawChinese("绊爱初号机最高");
isRawChinese("我完全爱上Warma了"); |
function chinese(str) { |
按照中文字符对应编写正则
/[\u4E00-\u9FFF\u3400-\u4DBF\uF900-\uFAFF\U00020000-U0002EBEF]/ |
function isChinese(str) { |
function reg(str){ |
// 是否全为中文
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;
} |
function CheckIsChinese(data) { function CheckIsAllChinese(data) { |
const isCN = str => /^[\u4e00-\u9fa5]+$/.test(str); |
// 判断字符是否是中文
|
第21天 写一个方法验证是否为中文
The text was updated successfully, but these errors were encountered: