考虑到是字符长度的判断;采用布尔的方式判断
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> </html> <script type="text/javascript"> console.log("是否是11位:", b("01234567891",11)); console.log("是否是2位:", b("01234567891",2)); function b(str, strlen){//字符,个数 var reg = "^[0-9]{" +strlen +"}$"; reg = new RegExp(reg); return reg.test(str); } </script>