RegExp.prototype.exec()

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

exec() 메소드는 어떤 문자열에서 정규표현식과 일치하는 문자열 검색을 수행한다. 결과로 배열을 리턴하거나 null을 반환합니다.

혹시 단순히 true 또는 false을 알아내기 위해 매칭을 수행한다면,  RegExp.prototype.test() 메소드 또는 String.prototype.search() 메소드를 사용하세요..

문법

regexObj.exec(str)

인자

str
정규표현식 검색을 수행할 대상 문자열

반환값

정규표현식 검색에 성공하면 배열을 반환하고 정규식표현식 객체의 속성들을 갱신합니다.
반환된 배열의 첫번째 원소는 정규표현식과 일치하는 문자열이고,  그 다음 원소들은 일치된 각각의 괄호에 대해서서 부합하는 문자열들입니다.

정규표현식 검색에 실패하면 null을 반환합니다.

설명

Consider the following example:

// Match "quick brown" followed by "jumps", ignoring characters in between
// Remember "brown" and "jumps"
// Ignore case
var re = /quick\s(brown).+?(jumps)/ig;
var result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog');

The following table shows the results for this script:

Object Property/Index Description Example
result [0] The full string of characters matched Quick Brown Fox Jumps
[1], ...[n ] The parenthesized substring matches, if any. The number of possible parenthesized substrings is unlimited. [1] = Brown
[2] = Jumps
index The 0-based index of the match in the string. 4
input The original string. The Quick Brown Fox Jumps Over The Lazy Dog
re lastIndex The index at which to start the next match. When "g" is absent, this will remain as 0. 25
ignoreCase Indicates if the "i" flag was used to ignore case. true
global Indicates if the "g" flag was used for a global match. true
multiline Indicates if the "m" flag was used to search in strings across multiple lines. false
source The text of the pattern. quick\s(brown).+?(jumps)

Examples

Finding successive matches

If your regular expression uses the "g" flag, you can use the exec() method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test() will also advance the lastIndex property). For example, assume you have this script:

var myRe = /ab*/g;
var str = 'abbcdefabh';
var myArray;
while ((myArray = myRe.exec(str)) !== null) {
  var msg = 'Found ' + myArray[0] + '. ';
  msg += 'Next match starts at ' + myRe.lastIndex;
  console.log(msg);
}

This script displays the following text:

Found abb. Next match starts at 3
Found ab. Next match starts at 9

Note: Do not place the regular expression literal (or RegExp constructor) within the while condition or it will create an infinite loop if there is a match due to the lastIndex property being reset upon each iteration. Also be sure that the global flag is set or a loop will occur here also.

Using exec() with RegExp literals

You can also use exec() without creating a RegExp object:

var matches = /(hello \S+)/.exec('This is a hello world!');
console.log(matches[1]);

This will log a message containing 'hello world!'.

Specifications

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.2.
ECMAScript 5.1 (ECMA-262)
The definition of 'RegExp.exec' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'RegExp.exec' in that specification.
Standard  
ECMAScript 2017 Draft (ECMA-262)
The definition of 'RegExp.exec' in that specification.
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

문서 태그 및 공헌자

태그: 
 최종 변경: dale0713,