2

我想更具体地说,将 replaceAll 与 GWT 一起使用:

doSomeGWTStuffWithTheString(text.replaceAll("(?i)(" + query + ")", "<b>$1</b>"));

但由于某种原因,它没有运行。我想我必须为此使用一些特殊的库。

如果您知道如何在 GWT 中执行上述操作,请告诉我。

我正在使用 GWT 的 2.4 beta 版本。

4

2 回答 2

5

String.replaceAll()在 GWT 客户端内部使用 Javascript 的 RegExp 实现。看看RegExp(包装类)javadoc。它说:

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.

所以似乎(?i)不支持使用。

于 2011-06-29T06:03:50.647 回答
2

如果那是您的整行,那么您忘记将返回的字符串分配给某些东西。该replaceAll()方法不会将替换结果隐式分配给其String对象。

测试:

String text = "I am trying to match SOMETHING";
String query = "ing";

System.out.println(text);
text = text.replaceAll("(?i)(" + query + ")", "<b>$1</b>");
System.out.println(text);

输出:

I am trying to match SOMETHING
I am try<b>ing</b> to match SOMETH<b>ING</b>
于 2011-06-29T03:45:59.287 回答