Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Strings and regular expressions

    I'm trying to do a very simple string replacement with regular expressions. But it does not seem to work!
    Are regular expressions supported as defined in java.util.regex.Pattern?
    Following is my code which doesn't work, as verified via debugger
    Code:
                    String url = currentLocation();
                    url.replaceAll("locale=.*", "locale=" + event.getValue());
    Thanx,

    Thomas

    PS: I'm using GWT 2.0.3 and SmartGWT 2.1
    Last edited by tdk; 17 May 2010, 04:27.

    #2
    Code:
    url = replaceAll(url, "locale=.*", "locale=" + event.getValue());
    
    public native String replaceAll(String s, String pattern, String replaceTxt) /*-{
        	var r = new RegExp(pattern, 'g');
        	return s.replace(r, replaceTxt);
    }-*/;

    Comment


      #3
      svjard,

      what exactly do you mean by these code snippets?
      Do I have to write this native method? Or is this the code from SmartGWT?

      If the latter, it does not really
      + answer my question if the regexp I can specify conforms to the java.util.regx.Pattern
      + it doesn't explain, why my code does not work (I stepped through it with the debugger and the replaceAll() did nothing).

      Confused,

      Thomas

      Comment


        #4
        There isn't a one-to-one correspondence between replaceAll in GWT and javascript because Java regular expressions do not neccessarily match Javascript regular expressions. Your code is fine, but won't work for that simple reason. So that code snippet was the native method to use and how to call it. It uses true Javascript regular expression syntax which will work.

        Comment


          #5
          thanx, that solved it.

          It might be a good idea to put that info somewhere in the documentation.

          Comment

          Working...
          X