先介绍java验证正则表达式的方法,一共有两种,代码如下:
package com.ustc.nhss.test; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @说明:java验证正则表达的式的两种方法 * */ public class Regular { public static void main(String args[]) { //第一种 Pattern p = Pattern.compile("^13\\d\\d\\d\\d\\d\\d\\d\\d\\d@e|(^15\\d\\d\\d\\d\\d\\d\\d\\d\\d@e)|(^18\\d\\d\\d\\d\\d\\d\\d\\d\\d@e)"); String s = "18668327308@e"; Matcher m = p.matcher(s); if (m.find()) { String x = s.substring(0, 11); System.out.println("{" + x + "}"); } else { System.out.println("xxx"); } String t = "^13\\d\\d\\d\\d\\d\\d\\d\\d\\d@e|(^15\\d\\d\\d\\d\\d\\d\\d\\d\\d@e)|(^18\\d\\d\\d\\d\\d\\d\\d\\d\\d@e)"; //第二种 if(s.matches(t)){ String x = s.substring(0, 11); System.out.println("{" + x + "}"); }; } }
以下为jquery验证ip的实例:
if(!valicateIp(deviceIp)){ alert("请输入合法的IP"); return false; } //检查IP的合法性 function valicateIp(ip){ var pattern = /((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/; return pattern.test(ip); }