regexgwtdev-mode

GWT Regex working in DevMode, not working in Production


I am using a RegEx that is working in DevMode but not once compiled and deployed. It is part of a class extending com.google.gwt.user.client.ui.SuggestOracle.Suggestion:

    @Override
public String getDisplayString() {
    String toReturn = myUser.getName() + " (" + myUser.getUserid() + ") " + (myUser.getCompanyname() == null ? "N/A" : myUser.getCompanyname());
    return toReturn.replaceAll("(?i)" + "(" + myInput + ")", "<b>$1</b>");
    return toReturn;
}

The official documentation (http://www.gwtproject.org/javadoc/latest/com/google/gwt/regexp/shared/RegExp.html) states the following:

There are a few small incompatibilities between the two implementations. Java-specific constructs in the regular expression syntax (e.g. [a-z&&[^bc]], (?<=foo), \A, \Q) work only in the pure Java implementation, not the GWT implementation, and are not rejected by either. Also, the Javascript-specific constructs $` and $' in the replacement expression work only in the GWT implementation, not the pure Java implementation, which rejects them.

Still, I don't know how to adapt my RegEx so that it works once deployed.

This site (http://planet.jboss.org/post/smartgwt_tip_regex_evaluation) says this:

The solution was to order the OR options from most complex to least complex

The Any ideas how to adapt this solution to my probem ?

Cheers, Tim


Solution

  • JavaScript does not support (?i) to switch to case-insensitive match.

    Your best bet is to use com.google.gwt.regexp.shared.RegExp:

    public String getDisplayString() {
      String toReturn = myUser.getName() + " (" + myUser.getUserid() + ") " + (myUser.getCompanyname() == null ? "N/A" : myUser.getCompanyname());
      return RegExp.compile(myInput, "ig").replace(toReturn, "<b>$&</b>");
    }