用正则表达式确实很简单
http://community.csdn.net/Expert/topic/3078/3078690.xml?temp=.5165827

解决方案 »

  1.   

    ip地址的校验法:<script> 
    function string_match(str){ 
    var reg = /^(d{1,3}).(d{1,3}).(d{1,3}).(d{1,3})$/ 
    var r = str.match(reg) 
    if(r==null)return false 
    if(r[1]>255||r[1]<0)return false 
    if(r[2]>255||r[2]<0)return false 
    if(r[3]>255||r[3]<0)return false 
    if(r[4]>255||r[4]<0)return false 
    return true 

    function regexp_exec(str){ 
    var reg = /^(d{1,3}).(d{1,3}).(d{1,3}).(d{1,3})$/ 
    if(reg.exec(str)==null)return false 
    if(RegExp.>255||RegExp.<0)return false 
    if(RegExp.>255||RegExp.<0)return false 
    if(RegExp.>255||RegExp.<0)return false 
    if(RegExp.>255||RegExp.<0)return false 
    return true 

    </script> 
    请输入IP:<input type=text value="127.0.0.1" id=ip_text> 检验方法: 
    <input type=button value=String对象的match onclick=alert(string_match(ip_text.value))> 
    <input type=button value=RegExp对象的exec onclick=alert(regexp_exec(ip_text.value))> 
    1.var r = str.match(reg) 
    使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回。 
    2.reg.exec(str) 
    如果 exec 方法没有找到匹配,则它返回 null。如果它找到匹配,则 exec 方法返回一个数组,并且更新全局 RegExp 对象的属性, 
    以反映匹配结果。数组的0元素包含了完整的匹配,而第1到n元素中包含的是匹配中出现的任意一个子匹配。如RegExp
      

  2.   

    呵呵,同意 ice_berg16(寻梦的稻草人) :)