Package com.google.gwt.regexp.shared
Class RegExp
java.lang.Object
com.google.gwt.regexp.shared.RegExp
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 TypeMethodDescriptionstatic RegExp
Creates a regular expression object from a pattern with no flags.static RegExp
Creates a regular expression object from a pattern using the given flags.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
Returns a literal patternString
for the specifiedString
.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.Splits the input string around matches of the regular expression.Splits the input string around matches of the regular expression.boolean
Determines if the regular expression matches the given string.
-
Method Details
-
compile
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
Creates a regular expression object from a pattern using the given flags.- Parameters:
pattern
- the Javascript regular expression pattern to compileflags
- 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
Returns a literal patternString
for the specifiedString
.This method produces a
Metacharacters or escape sequences in the input sequence will be given no special meaning.String
that can be used to create aRegExp
that would match the strings
as if it were a literal pattern.- Parameters:
input
- The string to be literalized- Returns:
- A literal string replacement
-
exec
Applies the regular expression to the given string. This call affects the value returned bygetLastIndex()
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 toexec(String)
ortest(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
Returns the pattern string of the regular expression. -
replace
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 $.
- 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
- ifreplacement
is invalid
-
setLastIndex
public void setLastIndex(int lastIndex) Sets the zero-based position at which to start the next match. -
split
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
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
Determines if the regular expression matches the given string. This call affects the value returned bygetLastIndex()
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.
-