Wednesday, November 19, 2014

How to use javascript's regular expression by jsni?

Hello:
     Similar question has been asked here: How to pass a regular expression as a function parameter, but I can't get it work by JSNI.

This is the string of the regular expression that will be used to test email:

      "^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"  

and if putting it to firebug execute like this:

     /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"/.test("test@domain.com")  

it will give what I want,but If wrapping it to a JSNI method:

    private native boolean reEmail()/-*{           return "^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$";      }-*/;  

then passing it to the function:

   private native boolean validate(String value, String regexp)/-*{         //escape special characters         var re=regexp.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').replace(/\x08/g, '\\x08');         return new RegExp(re,'g').test(value)     }-*/;  

like this:

      validate("test@domain.com",reEmail());  

It will give me false.Please tell what mistakes I have made,thanks.

No comments:

Post a Comment