You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expresi reguler merupakan cara yang kuat untuk mencari dan mengganti dalam teks.
4
4
5
-
<<<<<<< HEAD
6
-
Di JavaScript, mereka tersedia sebagai objek `RegExp`, dan bisa diintegrasi dalam metode string.
7
-
=======
8
-
In JavaScript, they are available as [RegExp](mdn:js/RegExp) object, and also integrated in methods of strings.
9
-
>>>>>>> a0bfa924a17cad8e7fee213904b27dbf57c2dbac
5
+
Di JavaScript, mereka tersedia sebagai objek [RegExp](mdn:js/RegExp), dan bisa diintegrasi dalam metode string.
10
6
11
7
## Expresi Reguler
12
8
@@ -27,71 +23,43 @@ regexp = /pattern/; // tanpa flag
27
23
regexp =/pattern/gmi; // dengan flag g,m dan i (untuk segera ditutup)
28
24
```
29
25
30
-
<<<<<<< HEAD
31
-
Garing miring `"/"` akan memberitahu JavaScript bahwa kita sedang membuat ekspresi reguler. Mereka memiliki peran yang sama seperti *quotes* untuk *strings*.
26
+
Garis miring `pattern:/.../` memberitahu JavaScript bahwa kita sedang membuat regular expression. Mereka memiliki peran yang sama dengan tanda petik untuk *string*.
32
27
33
-
## Penggunaan
28
+
Untuk kedua kasus `regexp` menjadi object kelas `RegExp` built-in.
34
29
35
-
Untuk mencari di dalam sebuah string, kita dapat menggunakan metode [*search*](mdn:js/String/search).
30
+
Perbedaan utama antara kedua syntax ini iadalah garis miring `pattern:/.../` melarang penyisipan expresi (seperti string dengan `${...}`). Mereka benar-benar static.
36
31
37
-
Berikut contohnya:
38
-
39
-
```js run
40
-
let str ="I love JavaScript!"; // akan mencari disini
41
-
=======
42
-
Slashes `pattern:/.../` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
43
-
44
-
In both cases `regexp` becomes an object of the built-in`RegExp`class.
45
-
46
-
The main difference between these two syntaxes is that slashes `pattern:/.../`do not allow to insert expressions (like strings with`${...}`). They are fully static.
47
-
48
-
Slashes are used when we know the regular expression at the code writing time -- and that's the most common situation. While `new RegExp` is used when we need to create a regexp "on the fly", from a dynamically generated string, for instance:
32
+
Garis miring digunakan saat kita tahu regular expression saat menulis kode -- dan itu situasi paling umum. Ketika `RegExp baru` digunakan saat kita harus membuat regexp baru "on the fly", dari string yang digenerate secara dinamis, misalnya:
49
33
50
34
```js
51
35
let tag =prompt("What tag do you want to find?", "h2");
52
-
>>>>>>> a0bfa924a17cad8e7fee213904b27dbf57c2dbac
53
36
54
-
let regexp = new RegExp(`<${tag}>`); // same as /<h2>/ if answered "h2" in the prompt above
37
+
let regexp =newRegExp(`<${tag}>`); //sama dengan /<h2>/ jika dijawab "h2" di prompt di atas
55
38
```
56
39
57
-
<<<<<<< HEAD
58
-
Methode `str.search` akan mencari pola `pattern:/love/` dan mengembalikan posisi `pattern:/love/` di string tersebut. Seperti yang kita duga, `pattern:/love/` adalah pola yang paling sederhana. Yang dilakukannya hanyalah mencari *substring* biasa.
59
-
60
-
Code di atas sama seperti berikut:
61
-
62
-
```js run
63
-
let str = "I love JavaScript!"; // akan mencari disini
64
-
=======
65
-
## Flags
40
+
## Flag
66
41
67
-
Regular expressions may have flags that affect the search.
42
+
Regular expression bisa punya flag yang mempengaruhi pencarian.
68
43
69
-
There are only 6 of them in JavaScript:
70
-
>>>>>>> a0bfa924a17cad8e7fee213904b27dbf57c2dbac
44
+
Cuma ada 6 di antaranya di JavaScript:
71
45
72
46
`pattern:i`
73
47
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
74
48
75
-
<<<<<<< HEAD
76
-
Jadi, mencari `pattern:/love/` sama seperti mencari `"love"`.
77
-
78
-
Tapi itu hanya untuk saat ini. Kita akan membuat ekspresi reguler yang lebih kompleks dengan kekuatan *search* yang jauh lebih banyak.
79
-
=======
80
49
`pattern:g`
81
-
: With this flag the search looks for all matches, without it -- only the first one.
50
+
: Dengan flag ini pencarian mencari semua kecocokan, tanpanya -- hanya yang pertama.
82
51
83
52
`pattern:m`
84
-
: Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
53
+
: Mode baris-ganda (dibahas di bab<info:regexp-multiline-mode>).
85
54
86
55
`pattern:s`
87
-
: Enables "dotall" mode, that allows a dot `pattern:.` to match newline character `\n` (covered in the chapter <info:regexp-character-classes>).
56
+
: Menyalakan mode "dotall", yang membolehkan dot `pattern:.`untuk cocok dengan karakter baris-baru `\n` (dibahas di bab<info:regexp-character-classes>).
88
57
89
58
`pattern:u`
90
-
: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
59
+
: Menyalakan dukungan penuh unicode. Flag ini menyalakan pemrosesan yang benar dari pasangan pengganti. Lebih lanjut tentang itu di bab<info:regexp-unicode>.
91
60
92
61
`pattern:y`
93
-
: "Sticky" mode: searching at the exact position in the text (covered in the chapter <info:regexp-sticky>)
94
-
>>>>>>> a0bfa924a17cad8e7fee213904b27dbf57c2dbac
62
+
: Mode "sticky": mencari posisi tepat di dalam teks (dibahas di bab <info:regexp-sticky>)
95
63
96
64
```smart header="Warna"
97
65
Skema warnanya adalah:
@@ -103,85 +71,38 @@ Skema warnanya adalah:
103
71
104
72
## Searching: str.match
105
73
106
-
<<<<<<< HEAD
107
-
````smart header="Kapan harus menggunakan `new RegExp`?"
108
-
Biasanya kita menggunakan sintaks pendek `/.../`. Tetapi itu tidak mendukung penyisipan variabel menggunakan sintaks `${...}`.
109
-
110
-
Di sisi lain, `new RegExp` memungkinkan untuk membangun pola secara dinamis dari string, jadi lebih fleksibel.
111
-
112
-
Berikut contoh dari *regexp* yang dihasilkan secara dinamis:
113
-
114
-
```js run
115
-
let tag = prompt("Tag mana yang ingin Kamu cari?", "h2");
116
-
let regexp = new RegExp(`<${tag}>`);
117
-
118
-
// mencari <h2> secara default
119
-
alert( "<h1> <h2> <h3>".search(regexp));
120
-
```
121
-
````
122
-
=======
123
-
As it was said previously, regular expressions are integrated with string methods.
74
+
Seperti yang dikatakan sebelumnya, regular expressions terintegrasi dengan metode string.
124
75
125
-
The method `str.match(regexp)` finds all matches of `regexp` in the string `str`.
76
+
Metode ini`str.match(regexp)`mencari semua kecocokan `regexp`di dalam string `str`.
126
77
127
-
It has 3 working modes:
78
+
Ia punya 3 mode kerja:
128
79
129
-
1. If the regular expression has flag `pattern:g`, it returns an array of all matches:
80
+
1.Jika regular expression punya flag `pattern:g`, ia mengembalikan array semua kecocokan:
130
81
```js run
131
82
let str ="We will, we will rock you";
132
83
133
-
alert( str.match(/we/gi) ); // We,we (an array of 2 substrings that match)
84
+
alert( str.match(/we/gi) ); // We,we (array 2 substring yang cocok)
134
85
```
135
-
Please note that both `match:We` and `match:we` are found, because flag `pattern:i` makes the regular expression case-insensitive.
136
-
>>>>>>> a0bfa924a17cad8e7fee213904b27dbf57c2dbac
86
+
Tolong ingat bahwa kedua `match:We` dan `match:we` ditemukan, karena flag `pattern:i` membuat regular expression case-insensitive.
137
87
138
88
2. If there's no such flag it returns only the first match in the form of an array, with the full match at index `0` and some additional details in properties:
139
89
```js run
140
90
let str = "We will, we will rock you";
141
91
142
-
<<<<<<< HEAD
143
-
## Flag
144
-
145
-
Expresi reguler mungkin memiliki *flag* yang dapat mempengaruhi *search*.
146
-
147
-
Mereka hanya ada 6 di Javascript:
148
-
149
-
`i`
150
-
: Dengan *flag* ini, *search* tidak peka terhadap huruf besar maupun kecil: tidak ada perbedaan antara `A` and `a` (lihat contohnya di bawah ini).
151
-
152
-
`g`
153
-
: Dengan *flag* ini, *search* akan mencari semua yang cocok dengannya, tanpa itu -- hanya yang pertama yang cocok (kita akan melihat kegunaan *flag* `g` di bab berikutnya).
154
-
155
-
`m`
156
-
: Mode *Multiline* (tercantum di dalam bab <info:regexp-multiline-mode>).
157
-
158
-
`s`
159
-
: Mode *Dotall*, memungkinkan `.` untuk cocok dengan baris baru (tecantum di dalam bab <info:regexp-character-classes>).
160
-
161
-
`u`
162
-
: Mengaktifkan dukungan unicode secara penuh. *Flag* ini memungkinkan untuk memproses pasangan pengganti (unicode) yang benar. Lebih lanjut tentangnya ada di dalam bab <info:regexp-unicode>.
163
-
164
-
`y`
165
-
: Mode *Sticky* (tercantum di dalam bab <info:regexp-sticky>)
166
-
167
-
Kita akan membahas semua *flag* tersebut lebih lanjut di dalam tutorial.
168
-
169
-
Untuk sekarang, *flag* paling sederhana adalah `i`, berikut contohnya:
170
-
=======
171
-
let result = str.match(/we/i); // without flag g
92
+
let result = str.match(/we/i); // tanpa flag g
172
93
173
94
alert( result[0] ); // We (1st match)
174
95
alert( result.length ); // 1
175
96
176
-
// Details:
177
-
alert( result.index ); // 0 (position of the match)
97
+
// Detil:
98
+
alert( result.index ); // 0 (posisi kecocokan)
178
99
alert( result.input ); // We will, we will rock you (source string)
179
100
```
180
101
The array may have other indexes, besides `0` if a part of the regular expression is enclosed in parentheses. We'll cover that in the chapter <info:regexp-groups>.
181
102
182
-
3. And, finally, if there are no matches, `null` is returned (doesn't matter if there's flag `pattern:g` or not).
103
+
3.Dan, akhirnya, jika tak ada kecocokan, `null`dikembalikan (tak peduli apakah ada flag `pattern:g`atau tidak).
183
104
184
-
That's a very important nuance. If there are no matches, we get not an empty array, but`null`. Forgetting about that may lead to errors, e.g.:
105
+
Itu nuansa paling penting. Jika tak ada kecocokan, kita tak mendapatkan array kosong, tapi`null`. Melupakan itu bisa membawa galat, misal:
185
106
186
107
```js run
187
108
let matches = "JavaScript".match(/HTML/); // = null
@@ -191,59 +112,50 @@ Untuk sekarang, *flag* paling sederhana adalah `i`, berikut contohnya:
191
112
}
192
113
```
193
114
194
-
If we'd like the result to be always an array, we can write it this way:
115
+
Jika kita mau hasilnya selalu array, kita bisa menulisnya seperti ini:
195
116
196
117
```js run
197
118
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
198
119
199
120
if (!matches.length) {
200
-
alert("No matches"); // now it works
121
+
alert("No matches"); // sekarang ini bekerja
201
122
}
202
123
```
203
124
204
-
## Replacing: str.replace
125
+
## Mengganti:str.replace
205
126
206
-
The method `str.replace(regexp, replacement)` replaces matches with `regexp` in string `str` with `replacement` (all matches, if there's flag `pattern:g`, otherwise only the first one).
127
+
Metode `str.replace(regexp, replacement)`mengganti kecocokan dengan`regexp`dalam string `str`dengan`replacement` (semua kecocokan, jika ada flag `pattern:g`, kalau tidak cuma yang pertama).
207
128
208
-
For instance:
209
-
>>>>>>> a0bfa924a17cad8e7fee213904b27dbf57c2dbac
129
+
Misalnya:
210
130
211
131
```js run
212
-
// no flag g
132
+
// tak ada flag g
213
133
alert( "We will, we will".replace(/we/i, "I") ); // I will, we will
214
134
215
-
// with flag g
135
+
// dengan flag g
216
136
alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will
217
137
```
218
138
219
-
The second argument is the `replacement`string. We can use special character combinations in it to insert fragments of the match:
alert( str.search(/LOVE/) ); // -1 (tidak ada yang ditemukan tanpa flag 'i')
225
-
```
139
+
Argumen kedua adalah string `replacement`. Kita bisa memakai kombinasi karakter spesial di dalamnya untuk menyisipkan fragment kecocokan:
226
140
227
-
*Flag*`i` telah membuat ekpresi reguler menjadi lebih kuat daripada *search substring* biasanya. Tapi masih ada banyak lagi. Kita akan membahas *flag* lain dan fitur *regxp* di dalam bab berikutnya.
228
-
=======
229
-
| Symbols | Action in the replacement string |
141
+
| Simbol | Aksi di string pengganti |
230
142
|--------|--------|
231
-
|`$&`|inserts the whole match|
232
-
|<code>$`</code>|inserts a part of the string before the match|
233
-
|`$'`|inserts a part of the string after the match|
143
+
|`$&`|menyisipkan seluruh kecocokan|
144
+
|<code>$`</code>|menyisipkan bagian string sebelum kecocokan|
145
+
|`$'`|menyisipkan bagian string setelah kecocokan|
234
146
|`$n`|if`n` is a 1-2 digit number, then it inserts the contents of n-th parentheses, more about it in the chapter <info:regexp-groups>|
235
-
|`$<name>`|inserts the contents of the parentheses with the given `name`, more about it in the chapter<info:regexp-groups>|
236
-
|`$$`|inserts character`$`|
147
+
|`$<name>`|menyisipkan konten tanda kurung dengan `name` yang diberikan, lebih lanjut tentang ini ada di bab<info:regexp-groups>|
148
+
|`$$`|menyisipkan karakter`$`|
237
149
238
-
An example with`pattern:$&`:
150
+
Contohnya dengan`pattern:$&`:
239
151
240
152
```js run
241
153
alert( "I love HTML".replace(/HTML/, "$& and JavaScript") ); // I love HTML and JavaScript
242
154
```
243
155
244
156
## Testing:regexp.test
245
157
246
-
The method `regexp.test(str)`looks for at least one match, if found, returns`true`, otherwise`false`.
158
+
Metode `regexp.test(str)`mencari paling tidak 1 kecocokan, jika ditemukan, mengembalikan`true`, kalau tidak`false`.
247
159
248
160
```js run
249
161
let str = "I love JavaScript";
@@ -252,21 +164,14 @@ let regexp = /LOVE/i;
252
164
alert( regexp.test(str) ); // true
253
165
```
254
166
255
-
Further in this chapter we'll study more regular expressions, come across many other examples and also meet other methods.
256
-
>>>>>>> a0bfa924a17cad8e7fee213904b27dbf57c2dbac
167
+
Lebih lanjut di bab ini kita akan mempelajari lebih regular expression, menjumpai banyak contoh dan menemui metode lainnya.
257
168
258
-
Full information about the methods is given in the article<info:regexp-methods>.
169
+
Informasi penuh tentang metode ini diberikan dalam artikel<info:regexp-methods>.
259
170
260
171
## Ringkasan
261
172
262
-
<<<<<<< HEAD
263
-
- Ekspresi reguler terdiri dari *pattern* dan opsional *flag*: `g`, `i`, `m`, `u`, `s`, `y`.
264
-
- Tanpa *flag* dan simbol khusus yang akan kita pelajari nanti, *search* dengan *regexp* sama seperti *search* dengan substring.
265
-
- Metode `str.search(regexp)` mengembalikan *index* yang cocok atau `-1` jika tidak ada yang cocok. Dalam bab selanjutnya kita akan melihat metode lain.
266
-
=======
267
-
- A regular expression consists of a pattern and optional flags: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
268
-
- Without flags and special symbols that we'll study later, the search by a regexp is the same as a substring search.
269
-
- The method `str.match(regexp)` looks for matches: all of them if there's `pattern:g` flag, otherwise only the first one.
270
-
- The method `str.replace(regexp, replacement)` replaces matches with `regexp` by `replacement`: all of them if there's `pattern:g` flag, otherwise only the first one.
271
-
- The method `regexp.test(str)` returns `true` if there's at least one match, otherwise `false`.
272
-
>>>>>>> a0bfa924a17cad8e7fee213904b27dbf57c2dbac
173
+
- Regular expression terdiri atas pola dan flag opsional:`pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
174
+
- Tanpa flag dan simbol spesial yang akan kita pelajari nanti, pencarian menggunakan regexp sama dengan pencarian substring.
175
+
- Metode `str.match(regexp)` mencari kecocokan: semuanya jika ada flag `pattern:g`, kalau tidak cuma yang pertama.
176
+
- Metode `str.replace(regexp, replacement)` mengganti kecocokan dengan `regexp` by `replacement`: semuanya jika ada flag `pattern:g`, selain itu cuma yang pertama.
177
+
- Metode `regexp.test(str)` mengembalikan `true` jika ada paling tidak satu kecocokan, kalau tidak `false`.
0 commit comments