diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
commit | da78a9e329e272dedb2400b79a3bdeebff387d47 (patch) | |
tree | e6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/ko/web/javascript/reference/global_objects/regexp/exec | |
parent | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff) | |
download | translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2 translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip |
initial commit
Diffstat (limited to 'files/ko/web/javascript/reference/global_objects/regexp/exec')
-rw-r--r-- | files/ko/web/javascript/reference/global_objects/regexp/exec/index.html | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/files/ko/web/javascript/reference/global_objects/regexp/exec/index.html b/files/ko/web/javascript/reference/global_objects/regexp/exec/index.html new file mode 100644 index 0000000000..4659688c51 --- /dev/null +++ b/files/ko/web/javascript/reference/global_objects/regexp/exec/index.html @@ -0,0 +1,188 @@ +--- +title: RegExp.prototype.exec() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/exec +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - 정규 표현식 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/exec +--- +<div>{{JSRef}}</div> + +<p><strong><code>exec()</code></strong> 메서드는 주어진 문자열에서 일치 탐색을 수행한 결과를 배열 혹은 {{jsxref("null")}}로 반환합니다.</p> + +<p>JavaScript {{jsxref("RegExp")}} 객체는 {{jsxref("RegExp.global", "global")}} 또는 {{jsxref("RegExp.sticky", "sticky")}} 플래그를 설정(<code>/foo/g</code>, <code>/foo/y</code> 등)한 경우 이전 일치의 인덱스를 저장하므로 <strong>상태를 가지고</strong>(stateful) 있습니다. 이를 내부적으로 사용하여, {{jsxref("String.prototype.match()")}}와는 다르게 (캡처 그룹을 포함한) 문자열 내의 일치 다수를 반복해 순회할 수 있습니다.</p> + +<p>(캡처 그룹을 포함한) 문자열 내의 다수 일치를 수행할 수 있는 보다 간편한 신규 메서드, {{jsxref("String.prototype.matchAll()")}}이 제안된 상태입니다.</p> + +<p>단순히 <code>true</code>/<code>false</code>가 필요한 경우 {{jsxref("RegExp.prototype.text()")}} 메서드 혹은 {{jsxref("String.prototype.search()")}}를 사용하세요.</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-exec.html")}}</div> + + + +<h2 id="구문">구문</h2> + +<pre class="syntaxbox notranslate"><var>regexObj</var>.exec(<var>str</var>)</pre> + +<h3 id="매개변수">매개변수</h3> + +<dl> + <dt><code>str</code></dt> + <dd>정규 표현식 검색을 수행할 대상 문자열.</dd> +</dl> + +<h3 id="반환_값">반환 값</h3> + +<p>정규 표현식이 일치하면, <code>exec()</code> 메서드는 배열(추가 속성 <code>index</code>와 <code>input</code> 포함, 아래 설명을 참고하세요)을 반환하고, 정규 표현식 객체의 {{jsxref("RegExp.lastIndex", "lastIndex")}} 속성을 업데이트합니다. 반환하는 배열은 일치한 텍스트를 첫 번째 원소로, 각각의 괄호 캡처 그룹을 이후 원소로 포함합니다.</p> + +<p>정규표현식 검색에 실패하면, <code>exec()</code> 메서드는 {{jsxref("null")}}을 반환하고 {{jsxref("RegExp.lastIndex", "lastIndex")}}를 <code>0</code>으로 설정합니다.</p> + +<h2 id="설명">설명</h2> + +<p>다음과 같은 예제를 고려해보세요.</p> + +<pre class="brush: js notranslate">// Match "quick brown" followed by "jumps", ignoring characters in between +// Remember "brown" and "jumps" +// Ignore case +let re = /quick\s(brown).+?(jumps)/ig; +let result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog');</pre> + +<p>다음의 표는 이 스크립트의 결과에 대해 보여줍니다.</p> + +<table class="fullwidth-table standard-table"> + <thead> + <tr> + <th scope="row">객체</th> + <th scope="col">속성/인덱스</th> + <th scope="col">설명</th> + <th scope="col">예제</th> + </tr> + </thead> + <tbody> + <tr> + <th colspan="1" rowspan="4" scope="row" style="vertical-align: top;"><code>result</code></th> + <td><code>[0]</code></td> + <td>일치한 전체 문자.</td> + <td><code>"Quick Brown Fox Jumps"</code></td> + </tr> + <tr> + <td><code>[1], ...[<var>n</var>]</code></td> + <td> + <p>(존재하는 경우) 괄호로 감싼 부분문자열.</p> + + <p>괄호로 감싼 부분문자열 숫자의 제한은 없습니다.</p> + </td> + <td> + <p><code>result[1] === "Brown"</code></p> + + <p><code>result[2] === "Jumps"</code></p> + </td> + </tr> + <tr> + <td><code>index</code></td> + <td>일치가 문자열에서 위치하는 인덱스. (0 시작)</td> + <td><code>4</code></td> + </tr> + <tr> + <td><code>input</code></td> + <td>원본 문자열.</td> + <td><code>"The Quick Brown Fox Jumps Over The Lazy Dog"</code></td> + </tr> + <tr> + <th colspan="1" rowspan="5" scope="row" style="vertical-align: top;"><code>re</code></th> + <td><code>lastIndex</code></td> + <td> + <p>다음 일치를 시작할 인덱스.</p> + + <p><code>g</code>를 누락하면 항상 <code>0</code>입니다.</p> + </td> + <td><code>25</code></td> + </tr> + <tr> + <td><code>ignoreCase</code></td> + <td><code>i</code> 플래그로 대소문자를 무시했는지 여부.</td> + <td><code>true</code></td> + </tr> + <tr> + <td><code>global</code></td> + <td><code>g</code> 플래그로 전역 일치를 수행하는지 여부.</td> + <td><code>true</code></td> + </tr> + <tr> + <td><code>multiline</code></td> + <td><code>m</code> 플래그로 여러 줄에 걸친 탐색을 수행하는지 여부.</td> + <td><code>false</code></td> + </tr> + <tr> + <td><code>source</code></td> + <td>패턴 문자열.</td> + <td><code>"quick\s(brown).+?(jumps)"</code></td> + </tr> + </tbody> +</table> + +<h2 id="예제">예제</h2> + +<h3 id="Finding_successive_matches">Finding successive matches</h3> + +<p>If your regular expression uses the "<code>g</code>" flag, you can use the <code>exec()</code> method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of <code>str</code> specified by the regular expression's {{jsxref("RegExp.lastIndex", "lastIndex")}} property ({{jsxref("RegExp.prototype.test()", "test()")}} will also advance the {{jsxref("RegExp.lastIndex", "lastIndex")}} property). For example, assume you have this script:</p> + +<pre class="brush: js notranslate">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); +} +</pre> + +<p>This script displays the following text:</p> + +<pre class="notranslate">Found abb. Next match starts at 3 +Found ab. Next match starts at 9 +</pre> + +<p>Note: Do not place the regular expression literal (or {{jsxref("RegExp")}} constructor) within the <code>while</code> condition or it will create an infinite loop if there is a match due to the {{jsxref("RegExp.lastIndex", "lastIndex")}} property being reset upon each iteration. Also be sure that the global flag is set or a loop will occur here also.</p> + +<h3 id="Using_exec_with_RegExp_literals">Using <code>exec()</code> with <code>RegExp</code> literals</h3> + +<p>You can also use <code>exec()</code> without creating a {{jsxref("RegExp")}} object:</p> + +<pre class="brush: js notranslate">var matches = /(hello \S+)/.exec('This is a hello world!'); +console.log(matches[1]); +</pre> + +<p>This will log a message containing 'hello world!'.</p> + +<h2 id="명세">명세</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype.exec', 'RegExp.exec')}}</td> + </tr> + </tbody> +</table> + +<h2 id="브라우저_호환성">브라우저 호환성</h2> + +<div> + + +<p>{{Compat("javascript.builtins.RegExp.exec")}}</p> +</div> + +<h2 id="같이_보기">같이 보기</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">Regular Expressions</a> chapter in the <a href="/en-US/docs/Web/JavaScript/Guide">JavaScript Guide</a></li> + <li>{{jsxref("RegExp")}}</li> +</ul> |