Following regex gives wrong result for ip address validation rvrn though regular expression is correct and gives correct result in node based running of .js code.
public static void excuteWithinEngine() throws ScriptException{
// ip address regex
String regEx = "^(?:\\d|1?\\d\\d|2[0-4]\\d|25[0-5])(?:\\.(?:\\d|1?\\d\\d|2[0-4]\\d|25[0-5])){3}$";
Set<String> in=new HashSet<String>();
//IPv4 A:B:C:D. if more than one chars are at position B,C then jsvascript based regex-test fails
in.add("10.2.2.2");//output: true. (correct result)
in.add("10.2.13.2");//output: false. (incorrect result)
in.add("10.2.13.25");//output: false. (incorrect result)
in.add("10.2.2.25");//output: true. (correct result)
for(String input:in){
String s = input;
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine se = factory.getEngineByName("JavaScript");
se.put("regEx", regEx);
se.put("string", s);
se.eval("var RE = new RegExp(regEx);");
boolean b= (boolean)se.eval("RE.test(string);"); // test function
System.out.println("Testing "+s+" :"+b);
}
}