var s = "2a3d";
var reg = new RegExp("["+ s +"]", "g");
var str = "weyusa";
alert(reg.test(str));

解决方案 »

  1.   

    <script>
    var str="hello2";
    if(/[2a3d]/g.test(str))
    alert('字符串包含非法字符');
    else
    alert('字符串合格');</script>
      

  2.   


    <html>
    <head>
    <script language="JavaScript" >
    function  checkOtherChar(str,errmsg) {
           for(var loop_index=0; loop_index<str.length; loop_index++)  
           {  
             if(str.charAt(loop_index) == '2'   
               ||str.charAt(loop_index) == 'a'  
               ||str.charAt(loop_index) == '3'  
               ||str.charAt(loop_index) == 'd'  
               ) 
              {  
                alert(errmsg);
                return false;  
           }  
             }//end of for(loop_index)  
          return true;
       }
     checkOtherChar('1111',"error");  
    </script>
    </head>
    <body>
    <form name="x" >
    </form></body>
    </html>