Class RegExp

java.lang.Object
com.google.gwt.regexp.shared.RegExp

public class RegExp extends Object
A class for regular expressions with features like Javascript's RegExp, plus Javascript String's replace and split methods (which can take a RegExp parameter). The pure Java implementation (for server-side use) uses Java's Pattern class, unavailable under GWT. The super-sourced GWT implementation simply calls on to the native Javascript classes.

There are a few small incompatibilities between the two implementations. Java-specific constructs in the regular expression syntax (e.g. [a-zinvalid input: '&'invalid input: '&'[^bc]], (?invalid input: '<'=foo), \A, \Q) work only in the pure Java implementation, not the GWT implementation, and are not rejected by either. Also, the Javascript-specific constructs $` and $' in the replacement expression work only in the GWT implementation, not the pure Java implementation, which rejects them.

  • Method Summary

    Modifier and Type
    Method
    Description
    static RegExp
    compile(String pattern)
    Creates a regular expression object from a pattern with no flags.
    static RegExp
    compile(String pattern, String flags)
    Creates a regular expression object from a pattern using the given flags.
    exec(String input)
    Applies the regular expression to the given string.
    boolean
    Returns whether the regular expression captures all occurrences of the pattern.
    boolean
    Returns whether the regular expression ignores case.
    int
    Returns the zero-based position at which to start the next match.
    boolean
    Returns whether '$' and '^' match line returns ('\n' and '\r') in addition to the beginning or end of the string.
    Returns the pattern string of the regular expression.
    static String
    quote(String input)
    Returns a literal pattern String for the specified String.
    replace(String input, String replacement)
    Returns the input string with the part(s) matching the regular expression replaced with the replacement string.
    void
    setLastIndex(int lastIndex)
    Sets the zero-based position at which to start the next match.
    split(String input)
    Splits the input string around matches of the regular expression.
    split(String input, int limit)
    Splits the input string around matches of the regular expression.
    boolean
    test(String input)
    Determines if the regular expression matches the given string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • compile

      public static RegExp compile(String pattern)
      Creates a regular expression object from a pattern with no flags.
      Parameters:
      pattern - the Javascript regular expression pattern to compile
      Returns:
      a new regular expression
      Throws:
      RuntimeException - if the pattern is invalid
    • compile

      public static RegExp compile(String pattern, String flags)
      Creates a regular expression object from a pattern using the given flags.
      Parameters:
      pattern - the Javascript regular expression pattern to compile
      flags - the flags string, containing at most one occurrence of 'g' (getGlobal()), 'i' (getIgnoreCase()), or 'm' (getMultiline()).
      Returns:
      a new regular expression
      Throws:
      RuntimeException - if the pattern or the flags are invalid
    • quote

      public static String quote(String input)
      Returns a literal pattern String for the specified String.

      This method produces a String that can be used to create a RegExp that would match the string s as if it were a literal pattern.

      Metacharacters or escape sequences in the input sequence will be given no special meaning.
      Parameters:
      input - The string to be literalized
      Returns:
      A literal string replacement
    • exec

      public MatchResult exec(String input)
      Applies the regular expression to the given string. This call affects the value returned by getLastIndex() if the global flag is set.
      Parameters:
      input - the string to apply the regular expression to
      Returns:
      a match result if the string matches, else null
    • getGlobal

      public boolean getGlobal()
      Returns whether the regular expression captures all occurrences of the pattern.
    • getIgnoreCase

      public boolean getIgnoreCase()
      Returns whether the regular expression ignores case.
    • getLastIndex

      public int getLastIndex()
      Returns the zero-based position at which to start the next match. The return value is not defined if the global flag is not set. After a call to exec(String) or test(String), this method returns the next position following the most recent match.
      See Also:
    • getMultiline

      public boolean getMultiline()
      Returns whether '$' and '^' match line returns ('\n' and '\r') in addition to the beginning or end of the string.
    • getSource

      public String getSource()
      Returns the pattern string of the regular expression.
    • replace

      public String replace(String input, String replacement)
      Returns the input string with the part(s) matching the regular expression replaced with the replacement string. If the global flag is set, replaces all matches of the regular expression. Otherwise, replaces the first match of the regular expression. As per Javascript semantics, backslashes in the replacement string get no special treatment, but the replacement string can use the following special patterns:
      • $1, $2, ... $99 - inserts the n'th group matched by the regular expression.
      • $& - inserts the entire string matched by the regular expression.
      • $$ - inserts a $.
      Note: $` and $' are *not* supported in the pure Java implementation, and throw an exception.
      Parameters:
      input - the string in which the regular expression is to be searched.
      replacement - the replacement string.
      Returns:
      the input string with the regular expression replaced by the replacement string.
      Throws:
      RuntimeException - if replacement is invalid
    • setLastIndex

      public void setLastIndex(int lastIndex)
      Sets the zero-based position at which to start the next match.
    • split

      public SplitResult split(String input)
      Splits the input string around matches of the regular expression. If the regular expression is completely empty, splits the input string into its constituent characters. If the regular expression is not empty but matches an empty string, the results are not well defined.
      Parameters:
      input - the string to be split.
      Returns:
      the strings split off, any of which may be empty.
    • split

      public SplitResult split(String input, int limit)
      Splits the input string around matches of the regular expression. If the regular expression is completely empty, splits the input string into its constituent characters. If the regular expression is not empty but matches an empty string, the results are not well defined. Note: There are some browser inconsistencies with this implementation, as it is delegated to the browser, and no browser follows the spec completely. A major difference is that IE will exclude empty strings in the result.
      Parameters:
      input - the string to be split.
      limit - the maximum number of strings to split off and return, ignoring the rest of the input string. If negative, there is no limit.
      Returns:
      the strings split off, any of which may be empty.
    • test

      public boolean test(String input)
      Determines if the regular expression matches the given string. This call affects the value returned by getLastIndex() if the global flag is set. Equivalent to: exec(input) != null
      Parameters:
      input - the string to apply the regular expression to
      Returns:
      whether the regular expression matches the given string.