验证字符串是否包含英文字符、数字或者汉字代码实例:
本章节分享一段代码实例它实现了能够验证字符串中是否包含英文字符、数字或者汉字的功能。
此函数比较灵活,能够进行定制,代码实例如下:
function done(input, withEnglishCharacter, withNumber, withChineseCharacter) { if (!Boolean(withEnglishCharacter) && !Boolean(withNumber) && !Boolean(withChineseCharacter)) { return false; //如果英文字母、数字和汉字都没有,则返回false } var pattern = '^['; if (Boolean(withEnglishCharacter)) { pattern += 'a-zA-Z'; } if (Boolean(withNumber)) { pattern += '0-9'; } if (Boolean(withChineseCharacter)) { pattern += '//u4E00-//u9FA5'; } pattern += ']+$'; var regex = new RegExp(pattern); if (input.match(regex)) { return true; } else { return false; } } var one = "antzone"; var two = "softwhy.com888"; var three = "蚂蚁部落good"; console.log(done(one, true, true, false)); console.log(done(two, false, true, false)); console.log(done(three, true, false, true));
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/8757.html