What is Java util. regex. Pattern. GWT substitution for quote (string ARG)
In my application, I need the following code:
string1. replaceAll(string2,myConstatntString)
The problem is that string1 and string2 can contain special symbols, such as' ('
I want to use Java util. regex. Pattern. Quote (string ARG) refers to string2:
string1. replaceAll(Pattern.quote(string2),myConstatntString);
But Java util. regex. Pattern is not available on the GWT client Does GWT have any replacement pattern quote?
Solution
I believe not, because JavaScript doesn't have its own method What you can do is use string Replace() instead of string Replaceall(), because you don't need regexp If you do, you will have to write your own method
This is done in javascript: is there a regexp escape function in Javascript?
This is how it is done in Java:
public static String quote(String s) { int slashEIndex = s.indexOf("\\E"); if (slashEIndex == -1) return "\\Q" + s + "\\E"; StringBuilder sb = new StringBuilder(s.length() * 2); sb.append("\\Q"); slashEIndex = 0; int current = 0; while ((slashEIndex = s.indexOf("\\E",current)) != -1) { sb.append(s.substring(current,slashEIndex)); current = slashEIndex + 2; sb.append("\\E\\\\E\\Q"); } sb.append(s.substring(current,s.length())); sb.append("\\E"); return sb.toString(); }
come from: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/regex/Pattern.java
(actual implementation in Java 1.5)