다음을 통해 공유


exec Method (Windows Scripting - JScript)

 

Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.

Syntax

rgExp.exec(str) 

Arguments

  • rgExp
    Required. An instance of a Regular Expression object containing the regular expression pattern and applicable flags.

  • str
    Required. The String object or string literal on which to perform the search.

Remarks

If the exec method does not find a match, it returns null. If it finds a match, exec returns an array, and the properties of the global RegExp object are updated to reflect the results of the match. Element zero of the array contains the entire match, while elements 1 – n contain any submatches that have occurred within the match. This behavior is identical to the behavior of the match method without the global flag (g) set.

If the global flag is set for a regular expression, exec searches the string beginning at the position indicated by the value of lastIndex. If the global flag is not set, exec ignores the value of lastIndex and searches from the beginning of the string.

The array returned by the exec method has three properties, input, index and lastIndex. The input property contains the entire searched string. The index property contains the position of the matched substring within the complete searched string. The lastIndex property contains the position following the last character in the match.

The following example illustrates the use of the exec method:

function RegExpTest()
{
   var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())
   if (ver < 5.5)
   {
      document.write("You need a newer version of JScript for this to work");
      return;
   }

   var src = "The quick brown fox jumps over the lazy dog.";

   // Create regular expression pattern with a global flag.
   var re = /\w+/g;

   // Get the next word, starting at the position of lastindex.
   var arr;
   while ((arr = re.exec(src)) != null)
      {
      // New line:
      document.write ("<br />");  
      document.write (arr.index + "-" + arr.lastIndex + " ");
      document.write (arr[0]);
      }
}

Requirements

Version 3

Applies To: Regular Expression Object (Windows Scripting - JScript)

Change History

Date

History

Reason

March 2009

Reorganized example code and changed literal.

Information enhancement.

See Also

match Method (Windows Scripting - JScript)
RegExp Object (Windows Scripting - JScript)
Regular Expression Syntax (Scripting)
search Method (Windows Scripting - JScript)
test Method (Windows Scripting - JScript)
Regular Expression Programming (Scripting)