Javascript验证字符串正则表达式
我有以下验证功能:Javascript验证字符串正则表达式,javascript,regex,Javascript,Regex,我有以下验证功能: function unformat (value) { if (!value) { return ''; } return value.replace(/^0+|[^0-9kK]+/g, '').toUpperCase(); } function validate(value) { const unformatted = unformat(value); if (/^0+/.test(unformatted)) { return f
function unformat (value) {
if (!value) {
return '';
}
return value.replace(/^0+|[^0-9kK]+/g, '').toUpperCase();
}
function validate(value) {
const unformatted = unformat(value);
if (/^0+/.test(unformatted)) {
return false;
}
let remainer = parseInt(unformatted.slice(0, -1), 10);
let module = 1;
let counter = 0;
while (remainer > 0) {
module = (module + (remainer % 10) * (9 - counter++ % 6)) % 11;
remainer = Math.floor(remainer / 10);
}
const verifier = module > 0 ? '' + (module - 1) : 'K';
return verifier === unformatted.slice(-1);
}
console.log(validate('14.211.109-8-')); // true, it should be false
console.log(validate('14.211.109-8 ')); // true, it should be false
在两种情况下,想法是不获取false
,而此时获取true
:
//空格结尾时如何修改该验证函数?您可以在函数开头添加检查:
if(/[ -]$/.test(value)) return false;
函数未格式化(值){
如果(!值){
返回“”;
}
返回值。替换(/^0+|[^0-9kK]+/g',).toUpperCase();
}
函数验证(值){
如果(/[-]$/.test(value))返回false;
常量未格式化=未格式化(值);
如果(/^0+/.test(未格式化)){
返回false;
}
设remainer=parseInt(未格式化的.slice(0,-1),10);
设模=1;
设计数器=0;
而(保留>0){
module=(module+(remainer%10)*(9-计数器+++%6))%11;
remainer=数学楼层(remainer/10);
}
常量验证器=模块>0?“”+(模块-1):“K”;
返回验证器===未格式化的.slice(-1);
}
console.log(验证('14.211.109-8-');//是的,它应该是假的
console.log(验证('14.211.109-8'));//如果为true,则应为false
您的代码应如下所示:
const validate = (value) => {
if (!/^.*[-\s]$/.test(value)) {
return false;
}
const unformatted = unformat(value);
let remainer = parseInt(unformatted.slice(0, -1), 10);
let module = 1;
let counter = 0;
while (remainer > 0) {
module = (module + (remainer % 10) * (9 - counter++ % 6)) % 11;
remainer = Math.floor(remainer / 10);
}
const verifier = module > 0 ? '' + (module - 1) : 'K';
return verifier === unformatted.slice(-1);
}
console.log(validate('14.211.109-8-')); // it should return false
console.log(validate('14.211.109-8 ')); // it should return false
console.log(validate('14.211.109-8')); // it should return true
如果您不想在开始处或结尾处留有空格或-空格,可以使用带有开始
^
和结束$
锚定的替代|
^ |[ -]$
如果要匹配空白字符,可以使用\s
,但请注意,它也可以匹配换行符
^\s|[\s-]$
比如说
if (/^ |[ -]$/.test(value)) {
return false;
}
函数未格式化(值){
如果(!值){
返回“”;
}
返回值。替换(/^0+|[^0-9kK]+/g',).toUpperCase();
}
函数验证(值){
如果(/^ |[-]$/.test(value)){
返回false;
}
常量未格式化=未格式化(值);
如果(/^0+/.test(未格式化)){
返回false;
}
设remainer=parseInt(未格式化的.slice(0,-1),10);
设模=1;
设计数器=0;
而(保留>0){
module=(module+(remainer%10)*(9-计数器+++%6))%11;
remainer=数学楼层(remainer/10);
}
常量验证器=模块>0?“”+(模块-1):“K”;
返回验证器===未格式化的.slice(-1);
}
console.log(验证('14.211.109-8-');//假的
console.log(验证('14.211.109-8'));//假的
console.log(验证('14.211.109-8'));//假的
console.log(验证('14.211.109-8'));//如果(/^.[-\s]$/.test(value))返回false,则返回true
?不,仍然返回true您需要将它放在函数validate(value){之后,如果开始时有空格?@pmiranda在这种情况下,您希望返回什么?这种情况下,也是False我选择了这个答案作为正确答案,但现在我正在尝试找到一种方法,在空格出现时返回False