<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js判断字符串是否有下划线</title> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> </head> <body> <script> var str="SH_20181030_1_25000"; var a=str.length; var str1 = str.replace(/\_+/g,"") var b=str1.length; var c=a-b; if(c!==3){ alert('名字下划线有问题'); }else{ alert("ok"); } </script> </body> </html>