Class RegExp


  • @JsType(isNative=true,
            namespace="<global>")
    public final class RegExp
    extends Object
    Regular expressions are patterns used to match character combinations in strings.
    In JavaScript, regular expressions are also objects.
    Author:
    Andrea "Stock" Stocchero
    • Constructor Summary

      Constructors 
      Constructor Description
      RegExp​(String pattern)
      Creates a regular expression.
      See MDN RegExp.
      RegExp​(String pattern, String flags)
      Creates a regular expression, using flags passed as string.
      If specified, flags is a string that contains the flags to add.
      Alternatively, if an object is supplied for the pattern, the flags string will replace any of that object's flags (and lastIndex will be reset to 0).
      Flags may contain any combination of the following characters:
      g (global match), finds all matches rather than stopping after the first match.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      RegExpResult exec​(String s)
      Executes a search for a match in a string.
      It returns an array of information or null on a mismatch.
      static int getLastIndex()
      Returns the index at which to start the next match.
      This property is set only if the regular expression instance used the g flag to indicate a global search, or the y flag to indicate a sticky search.
      The following rules apply:
      If lastIndex is greater than the length of the string, test(String) and exec(String) fail, then lastIndex is set to 0 If lastIndex is equal to or less than the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting from lastIndex.
      String getSource()
      Returns a string containing the source text of the RegExp object, and it doesn't contain the two forward slashes on both sides and any flags.
      boolean isDotAll()
      Returns true whether or not the "s" flag is used with the regular expression.
      It is a read-only property of an individual regular expression instance.
      boolean isGlobal()
      Returns true whether or not the "g" flag is used with the regular expression.
      It is a read-only property of an individual regular expression instance.
      boolean isIgnoreCase()
      Returns true if the "i" flag was used; otherwise, false.
      The "i" flag indicates that case should be ignored while attempting a match in a string.
      It is a read-only property of an individual regular expression instance.
      boolean isMultiline()
      Returns true whether or not the "m" flag is used with the regular expression.
      It is a read-only property of an individual regular expression instance.
      boolean isSticky()
      Returns true whether or not the search is sticky (searches in strings only from the index indicated by the last index property of this regular expression).
      It is a read-only property of an individual regular expression instance.
      boolean isUnicode()
      Returns true whether or not the "u" flag is used with a regular expression.
      It is a read-only property of an individual regular expression instance.
      boolean test​(String value)
      Executes a search for a match between a regular expression and a specified string.
    • Constructor Detail

      • RegExp

        public RegExp​(String pattern)
        Creates a regular expression.
        See MDN RegExp.
        Parameters:
        pattern - the text of the regular expression.
        Patterns can include special characters so they can match a wider range of values than would a literal string.
      • RegExp

        public RegExp​(String pattern,
                      String flags)
        Creates a regular expression, using flags passed as string.
        If specified, flags is a string that contains the flags to add.
        Alternatively, if an object is supplied for the pattern, the flags string will replace any of that object's flags (and lastIndex will be reset to 0).
        Flags may contain any combination of the following characters:
        • g (global match), finds all matches rather than stopping after the first match.
        • i (ignore case), if u flag is also enabled, uses Unicode case folding.
        • m (multiline) treats beginning and end characters (^ and $) as working over multiple lines.
          In other words, match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string.
        • s ("dotAll") allows . to match newlines.
        • u (unicode) treats pattern as a sequence of Unicode code points.
        • y (sticky) matches only from the index indicated by the lastIndex property of this regular expression in the target string.
          Does not attempt to match from any later indexes.

        See MDN RegExp.
        Parameters:
        pattern - the text of the regular expression.
        Patterns can include special characters so they can match a wider range of values than would a literal string.
        flags - If specified, flags is a string that contains the flags to add.
        Alternatively, if an object is supplied for the pattern, the flags string will replace any of that object's flags (and lastIndex will be reset to 0).
        Flags may contain any combination of the following characters:
        • g (global match), finds all matches rather than stopping after the first match.
        • i (ignore case), if u flag is also enabled, uses Unicode case folding.
        • m (multiline) treats beginning and end characters (^ and $) as working over multiple lines.
          In other words, match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string.
        • s ("dotAll") allows . to match newlines.
        • u (unicode) treats pattern as a sequence of Unicode code points.
        • y (sticky) matches only from the index indicated by the lastIndex property of this regular expression in the target string.
          Does not attempt to match from any later indexes.
    • Method Detail

      • getLastIndex

        @JsProperty
        public static int getLastIndex()
        Returns the index at which to start the next match.
        This property is set only if the regular expression instance used the g flag to indicate a global search, or the y flag to indicate a sticky search.
        The following rules apply:
        • If lastIndex is greater than the length of the string, test(String) and exec(String) fail, then lastIndex is set to 0
        • If lastIndex is equal to or less than the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting from lastIndex.
        • If lastIndex is equal to the length of the string and if the regular expression does not match the empty string, then the regular expression mismatches input, and lastIndex is reset to 0.
        • Otherwise, lastIndex is set to the next position following the most recent match.
        Returns:
        the index at which to start the next match
      • isDotAll

        @JsProperty
        public boolean isDotAll()
        Returns true whether or not the "s" flag is used with the regular expression.
        It is a read-only property of an individual regular expression instance.
        Returns:
        true whether or not the "s" flag is used with the regular expression
      • isGlobal

        @JsProperty
        public boolean isGlobal()
        Returns true whether or not the "g" flag is used with the regular expression.
        It is a read-only property of an individual regular expression instance.
        Returns:
        true whether or not the "g" flag is used with the regular expression
      • isIgnoreCase

        @JsProperty
        public boolean isIgnoreCase()
        Returns true if the "i" flag was used; otherwise, false.
        The "i" flag indicates that case should be ignored while attempting a match in a string.
        It is a read-only property of an individual regular expression instance.
        Returns:
        true if the "i" flag was used; otherwise, false
      • isMultiline

        @JsProperty
        public boolean isMultiline()
        Returns true whether or not the "m" flag is used with the regular expression.
        It is a read-only property of an individual regular expression instance.
        Returns:
        true whether or not the "m" flag is used with the regular expression
      • isUnicode

        @JsProperty
        public boolean isUnicode()
        Returns true whether or not the "u" flag is used with a regular expression.
        It is a read-only property of an individual regular expression instance.
        Returns:
        true whether or not the "u" flag is used with a regular expression
      • isSticky

        @JsProperty
        public boolean isSticky()
        Returns true whether or not the search is sticky (searches in strings only from the index indicated by the last index property of this regular expression).
        It is a read-only property of an individual regular expression instance.
        Returns:
        true whether or not the search is sticky (searches in strings only from the index indicated by the last index property of this regular expression)
      • getSource

        @JsProperty
        public String getSource()
        Returns a string containing the source text of the RegExp object, and it doesn't contain the two forward slashes on both sides and any flags.
        Returns:
        a string containing the source text of the RegExp object, and it doesn't contain the two forward slashes on both sides and any flags
      • exec

        @JsMethod
        public RegExpResult exec​(String s)
        Executes a search for a match in a string.
        It returns an array of information or null on a mismatch.
        Parameters:
        s - string to use for matching
        Returns:
        an array of information or null on a mismatch.
      • test

        @JsMethod
        public boolean test​(String value)
        Executes a search for a match between a regular expression and a specified string.
        Parameters:
        value - the string against which to match the regular expression.
        Returns:
        true if there is a match between the regular expression and the string. Otherwise, false.