aboutsummaryrefslogtreecommitdiff
path: root/files/id/web/javascript/reference/global_objects/regexp/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/id/web/javascript/reference/global_objects/regexp/index.html')
-rw-r--r--files/id/web/javascript/reference/global_objects/regexp/index.html231
1 files changed, 231 insertions, 0 deletions
diff --git a/files/id/web/javascript/reference/global_objects/regexp/index.html b/files/id/web/javascript/reference/global_objects/regexp/index.html
new file mode 100644
index 0000000000..8bd6dce4b9
--- /dev/null
+++ b/files/id/web/javascript/reference/global_objects/regexp/index.html
@@ -0,0 +1,231 @@
+---
+title: RegExp
+slug: Web/JavaScript/Reference/Global_Objects/RegExp
+translation_of: Web/JavaScript/Reference/Global_Objects/RegExp
+---
+<div>{{JSRef}}</div><p>The <strong><code>RegExp</code></strong>objek digunakan untuk pencocokan teks dengan pola.</p>
+
+<p>Untuk pengantar ekspresi reguler, baca <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">bab Ekspresi Reguler</a> di <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">Panduan JavaScript</a> .</p>
+
+<h2 id="Deskripsi">Deskripsi</h2>
+
+<h3 id="Notasi_dan_konstruktor_literal">Notasi dan konstruktor literal</h3>
+
+<p>Ada dua cara untuk membuat <code>RegExp</code>objek: <em>notasi literal</em> dan <em>konstruktor</em> .</p>
+
+<ul>
+ <li>Parameter <strong>notasi literal</strong> terlampir di antara garis miring dan tidak menggunakan tanda kutip.</li>
+ <li>Parameter <strong>fungsi konstruktor</strong> tidak tertutup di antara garis miring tetapi menggunakan tanda kutip.</li>
+</ul>
+
+<p>Tiga ekspresi berikut membuat ekspresi reguler yang sama:</p>
+
+<pre class="notranslate"><code>/ab+c/i
+new RegExp(/ab+c/, 'i') // literal notation
+new RegExp('ab+c', 'i') // constructor
+</code></pre>
+
+<p>Notasi literal menghasilkan kompilasi dari ekspresi reguler ketika ekspresi dievaluasi. Gunakan notasi literal ketika ekspresi reguler akan tetap konstan. Misalnya, jika Anda menggunakan notasi literal untuk membuat ekspresi reguler yang digunakan dalam satu lingkaran, ekspresi reguler tidak akan dikompilasi ulang pada setiap iterasi.</p>
+
+<p>Konstruktor objek ekspresi reguler — misalnya, <code>new RegExp('ab+c')</code>—menghasilkan kompilasi runtime dari ekspresi reguler. Gunakan fungsi konstruktor ketika Anda tahu pola ekspresi reguler akan berubah, atau Anda tidak tahu pola dan mendapatkannya dari sumber lain, seperti input pengguna.</p>
+
+<h3 id="Bendera_dalam_konstruktor">Bendera dalam konstruktor</h3>
+
+<p>Dimulai dengan ECMAScript 6, <code>new RegExp(/ab+c/, 'i')</code>tidak lagi melempar a <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError"><code>TypeError</code></a>( <code>"can't supply flags when constructing one RegExp from another"</code>) ketika argumen pertama adalah a <code>RegExp</code>dan <code><var>flags</var></code>argumen kedua hadir. Sebagai <code>RegExp</code>gantinya, argumen baru dibuat.</p>
+
+<p>Saat menggunakan fungsi konstruktor, aturan pelolosan string normal (mendahului karakter khusus <code>\</code>ketika disertakan dalam string) diperlukan.</p>
+
+<p>Misalnya, yang berikut ini setara:</p>
+
+<pre class="notranslate"><code>let re = /\w+/
+let re = new RegExp('\\w+')
+</code></pre>
+
+<h3 id="Properti_Reg-suka_seperti_Perl">Properti Reg-suka seperti Perl</h3>
+
+<p>Perhatikan bahwa beberapa <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp"><code>RegExp</code></a>properti memiliki nama panjang dan pendek (seperti Perl). Kedua nama selalu merujuk pada nilai yang sama. (Perl adalah bahasa pemrograman tempat JavaScript memodelkan ekspresi regulernya.). Lihat juga <a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#RegExp_Properties">properti yang </a><a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#RegExp_Properties">sudah usang <code>RegExp</code>.</a></p>
+
+<h2 id="Konstruktor">Konstruktor</h2>
+
+<dl>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp"><code>RegExp()</code></a></dt>
+ <dd>Menciptakan <code>RegExp</code>objek baru .</dd>
+</dl>
+
+<h2 id="Sifat_statis">Sifat statis</h2>
+
+<dl>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@species"><code>get RegExp[@@species]</code></a></dt>
+ <dd>The constructor function that is used to create derived objects.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex"><code>RegExp.lastIndex</code></a></dt>
+ <dd>The index at which to start the next match.</dd>
+</dl>
+
+<h2 id="Instance_properties">Instance properties</h2>
+
+<dl>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags"><code>RegExp.prototype.flags</code></a></dt>
+ <dd>A string that contains the flags of the <code>RegExp</code> object.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll"><code>RegExp.prototype.dotAll</code></a></dt>
+ <dd>Whether <code>.</code> matches newlines or not.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global"><code>RegExp.prototype.global</code></a></dt>
+ <dd>Whether to test the regular expression against all possible matches in a string, or only against the first.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase"><code>RegExp.prototype.ignoreCase</code></a></dt>
+ <dd>Whether to ignore case while attempting a match in a string.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline"><code>RegExp.prototype.multiline</code></a></dt>
+ <dd>Whether or not to search in strings across multiple lines.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source"><code>RegExp.prototype.source</code></a></dt>
+ <dd>The text of the pattern.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky"><code>RegExp.prototype.sticky</code></a></dt>
+ <dd>Whether or not the search is sticky.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode"><code>RegExp.prototype.unicode</code></a></dt>
+ <dd>Whether or not Unicode features are enabled.</dd>
+</dl>
+
+<h2 id="Instance_methods">Instance methods</h2>
+
+<dl>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/compile"><code>RegExp.prototype.compile()</code></a></dt>
+ <dd>(Re-)compiles a regular expression during execution of a script.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec"><code>RegExp.prototype.exec()</code></a></dt>
+ <dd>Executes a search for a match in its string parameter.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test"><code>RegExp.prototype.test()</code></a></dt>
+ <dd>Tests for a match in its string parameter.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/toString"><code>RegExp.prototype.toString()</code></a></dt>
+ <dd>Returns a string representing the specified object. Overrides the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString"><code>Object.prototype.toString()</code></a> method.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@match"><code>RegExp.prototype[@@match]()</code></a></dt>
+ <dd>Performs match to given string and returns match result.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll"><code>RegExp.prototype[@@matchAll]()</code></a></dt>
+ <dd>Returns all matches of the regular expression against a string.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@replace"><code>RegExp.prototype[@@replace]()</code></a></dt>
+ <dd>Replaces matches in given string with new substring.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@search"><code>RegExp.prototype[@@search]()</code></a></dt>
+ <dd>Searches the match in given string and returns the index the pattern found in the string.</dd>
+ <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@split"><code>RegExp.prototype[@@split]()</code></a></dt>
+ <dd>Splits given string into an array by separating the string into substring.</dd>
+</dl>
+
+<h2 id="Examples">Examples</h2>
+
+<h3 id="Using_a_regular_expression_to_change_data_format">Using a regular expression to change data format</h3>
+
+<p>The following script uses the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace"><code>replace()</code></a> method of the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String"><code>String</code></a> instance to match a name in the format <em>first last</em> and output it in the format <em>last, first</em>.</p>
+
+<p>In the replacement text, the script uses <code>$1</code> and <code>$2</code> to indicate the results of the corresponding matching parentheses in the regular expression pattern.</p>
+
+<pre class="notranslate"><code>let re = /(\w+)\s(\w+)/
+let str = 'John Smith'
+let newstr = str.replace(re, '$2, $1')
+console.log(newstr)
+</code></pre>
+
+<p>This displays <code>"Smith, John"</code>.</p>
+
+<h3 id="Using_regular_expression_to_split_lines_with_different_line_endingsends_of_lineline_breaks">Using regular expression to split lines with different line endings/ends of line/line breaks</h3>
+
+<p>The default line ending varies depending on the platform (Unix, Windows, etc.). The line splitting provided in this example works on all platforms.</p>
+
+<pre class="notranslate"><code>let text = 'Some text\nAnd some more\r\nAnd yet\rThis is the end'
+let lines = text.split(/\r\n|\r|\n/)
+console.log(lines) // logs [ 'Some text', 'And some more', 'And yet', 'This is the end' ]
+</code></pre>
+
+<p>Note that the order of the patterns in the regular expression matters.</p>
+
+<h3 id="Using_regular_expression_on_multiple_lines">Using regular expression on multiple lines</h3>
+
+<pre class="notranslate"><code>let s = 'Please yes\nmake my day!'
+
+s.match(/yes.*day/);
+// Returns null
+
+s.match(/yes[^]*day/);
+// Returns ["yes\nmake my day"]
+</code></pre>
+
+<h3 id="Using_a_regular_expression_with_the_sticky_flag">Using a regular expression with the sticky flag</h3>
+
+<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky"><code>sticky</code></a> flag indicates that the regular expression performs sticky matching in the target string by attempting to match starting at <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex"><code>RegExp.prototype.lastIndex</code></a>.</p>
+
+<pre class="notranslate"><code>let str = '#foo#'
+let regex = /foo/y
+
+regex.lastIndex = 1
+regex.test(str) // true
+regex.lastIndex = 5
+regex.test(str) // false (lastIndex is taken into account with sticky flag)
+regex.lastIndex // 0 (reset after match failure)</code></pre>
+
+<h3 id="The_difference_between_the_sticky_flag_and_the_global_flag">The difference between the sticky flag and the global flag</h3>
+
+<p>With the sticky flag <code>y</code>, the next match has to happen at the <code>lastIndex</code> position, while with the global flag <code>g</code>, the match can happen at the <code>lastIndex</code> position or later:</p>
+
+<pre class="notranslate"><code>re = /\d/y;
+while (r = re.exec("123 456")) console.log(r, "AND re.lastIndex", re.lastIndex);
+
+// [ '1', index: 0, input: '123 456', groups: undefined ] AND re.lastIndex 1
+// [ '2', index: 1, input: '123 456', groups: undefined ] AND re.lastIndex 2
+// [ '3', index: 2, input: '123 456', groups: undefined ] AND re.lastIndex 3
+// ... and no more match.</code></pre>
+
+<p>With the global flag <code>g</code>, all 6 digits would be matched, not just 3.</p>
+
+<h3 id="Regular_expression_and_Unicode_characters">Regular expression and Unicode characters</h3>
+
+<p>As mentioned above, <code>\w</code> or <code>\W</code> only matches ASCII based characters; for example, <code>a</code> to <code>z</code>, <code>A</code> to <code>Z</code>, <code>0</code> to <code>9</code>, and <code>_</code>.</p>
+
+<p>To match characters from other languages such as Cyrillic or Hebrew, use <code>\u<var>hhhh</var></code>, where <code><var>hhhh</var></code> is the character's Unicode value in hexadecimal.</p>
+
+<p>This example demonstrates how one can separate out Unicode characters from a word.</p>
+
+<pre class="notranslate"><code>let text = 'Образец text на русском языке'
+let regex = /[\u0400-\u04FF]+/g
+
+let match = regex.exec(text)
+console.log(match[0]) // logs 'Образец'
+console.log(regex.lastIndex) // logs '7'
+
+let match2 = regex.exec(text)
+console.log(match2[0]) // logs 'на' [did not log 'text']
+console.log(regex.lastIndex) // logs '15'
+
+// and so on
+</code></pre>
+
+<p>The <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes">Unicode property escapes</a> feature introduces a solution, by allowing for a statement as simple as <code>\p{scx=Cyrl}</code>. One can also use an external resource for getting the complete Unicode block range for different scripts, such as <a href="http://kourge.net/projects/regexp-unicode-block" rel="noopener">Regexp-Unicode-block</a>.</p>
+
+<h3 id="Extracting_sub-domain_name_from_URL">Extracting sub-domain name from URL</h3>
+
+<pre class="notranslate"><code>let url = 'http://xxx.domain.com'
+console.log(/[^.]+/.exec(url)[0].substr(7)) // logs 'xxx'
+</code></pre>
+
+<p>Instead of using regular expressions for parsing URLs, it is usually better to use the browsers built-in URL parser by using the <a href="/en-US/docs/Web/API/URL_API">URL API</a>.</p>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table>
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td><a href="https://tc39.es/ecma262/#sec-regexp-regular-expression-objects" hreflang="en" lang="en" rel="noopener">ECMAScript (ECMA-262)<br>
+ <small lang="en-US">The definition of 'RegExp' in that specification.</small></a></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<p>{{Compat("javascript.builtins.RegExp")}}</p>
+
+<h2 id="See_also">See also</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><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match"><code>String.prototype.match()</code></a></li>
+ <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace"><code>String.prototype.replace()</code></a></li>
+</ul>