Regex: Java or Javascript or a bit of both?

220 views
Skip to first unread message

Fred Janon

unread,
Aug 1, 2008, 12:06:47 AM8/1/08
to Google-We...@googlegroups.com
I don't quite understand if we need to use a Java regex or a Javascript one, the following sentence is ambigous to me:

http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.DeveloperGuide.Fundamentals.html

Runtime Library Support

...
  • Regular Expressions
    The syntax of Java regular expressions is similar, but not identical, to JavaScript regular expressions. For example, the replaceAll and split methods use regular expressions. So, you'll probably want to be careful to only use Java regular expressions that have the same meaning in JavaScript. <--???????????
How can we make sure of that? I wouldn't want to test an application and then discover that the regex doesn't work after deployment.

Thanks

Fred

Brian

unread,
Aug 1, 2008, 12:35:10 AM8/1/08
to Google Web Toolkit
GWT compiles your java into javascript. What you will be testing and
deploying is javascript. Java Source---GWT--->Javascript Output

I take that sentence to mean that you want to make sure the pattern
you're using in a regex is the Javascript meaning. Since your java
source is churned into javascript by gwt, a regex string literal in
your source code will become a regex string literal -in javascript-.
You might expect \Q \E to work according to the Java docs, but those
patterns aren't in Javascript's regex, so it won't work. I believe
the warning that you use expressions in Java that have the same
Javascript meaning is so you don't get confused looking at your source
code expecting a certain behavior (ie, the \Q \E above) and not seeing
it in the running code.


On Aug 1, 12:06 am, "Fred Janon" <fja...@gmail.com> wrote:
> I don't quite understand if we need to use a Java regex or a Javascript one,
> the following sentence is ambigous to me:
>
> http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.De...
>
> Runtime Library Support...
>
>    - Regular Expressions
>     The syntax of Java regular
> expressions<http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html>is
> similar, but not identical, to JavaScript
>    regular expressions<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Regula...>.
>    For example, the replaceAll and split methods use regular expressions. *So,
>    you'll probably want to be careful to only use Java regular expressions that
>    have the same meaning in JavaScript. <--???????????
>    *

Thomas Broyer

unread,
Aug 1, 2008, 6:53:38 AM8/1/08
to Google Web Toolkit


On Aug 1, 6:35 am, Brian <hibr...@gmail.com> wrote:
> GWT compiles your java into javascript.  What you will be testing and
> deploying is javascript.  Java Source---GWT--->Javascript Output
>
> I take that sentence to mean that you want to make sure the pattern
> you're using in a regex is the Javascript meaning.  Since your java
> source is churned into javascript by gwt, a regex string literal in
> your source code will become a regex string literal -in javascript-.
> You might expect \Q \E to work according to the Java docs, but those
> patterns aren't in Javascript's regex, so it won't work.  I believe
> the warning that you use expressions in Java that have the same
> Javascript meaning is so you don't get confused looking at your source
> code expecting a certain behavior (ie, the \Q \E above) and not seeing
> it in the running code.

...and the GWTShell executes your *Java* code, so the regexps have to
be Java-regexp-compatible for your app to work in Hosted Mode.

In a few words:
- if you use JavaScript-only regexp constructs, your app will fail in
Hosted Mode, so developping and debugging will become a pain
- if you use Java-only regexp constructs, your app will run in Hosted
Mode but will fail in "web mode"

So you should base your developments on the JavaScript regexp syntax;
and if it fails in Hosted Mode, then find an alternate that's still
JavaScript-compaible and happens to also be Java-compatible. If you
base your devs on the Java regexp syntax, you'll only notice the
incompatibilities when testing in web mode, which generally happen
late in the development process...

Fred Janon

unread,
Aug 1, 2008, 7:06:39 AM8/1/08
to Google-We...@googlegroups.com
Thanks Brian.
Thanks Thomas, you confirmed what I thought. I think that would deserve to be added to the GWT docs. I couldn't find it anywhere, maybe I missed it.

Fred

Brian

unread,
Aug 1, 2008, 8:02:18 AM8/1/08
to Google Web Toolkit
I'd be nervous about waiting until late in the cycle to test in 'web'
mode. I test webmode at least once a day, at least to run through
browsers to make sure the css still works. Anyway, yeah, it's a good
point that if you're doing most of the testing with the hosted mode,
that it's testing the java code.
Reply all
Reply to author
Forward
0 new messages