我想更具体地将replaceAll与GWT一起使用:
doSomeGWTStuffWithTheString(text.replaceAll("(?i)(" + query + ")", "<b>$1</b>"));
但由于某种原因它不会运行。我想我必须使用一些特殊的库。
如果您知道如何在GWT中执行上述操作,请告诉我们。
我正在使用GWT的2.4 beta版。
答案 0 :(得分:5)
String.replaceAll()
在内部使用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)
。
答案 1 :(得分: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>