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
Copy file name to clipboardExpand all lines: 9-regular-expressions/01-regexp-introduction/article.md
+2-56Lines changed: 2 additions & 56 deletions
Original file line number
Diff line number
Diff line change
@@ -1,36 +1,22 @@
1
1
# Pattern dan flag
2
2
3
-
<<<<<<< HEAD
4
3
Expresi reguler merupakan cara yang kuat untuk mencari dan mengganti dalam teks.
5
4
6
5
Di JavaScript, mereka tersedia sebagai objek [RegExp](mdn:js/RegExp), dan bisa diintegrasi dalam metode string.
7
-
=======
8
-
Regular expressions are patterns that provide a powerful way to search and replace in text.
9
-
10
-
In JavaScript, they are available via the [RegExp](mdn:js/RegExp) object, as well as being integrated in methods of strings.
11
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
12
6
13
7
## Expresi Reguler
14
8
15
9
Expresi reguler ("regexp", atau hanya "reg") terdiri dari *pola* dan *flag* opsional.
16
10
17
-
<<<<<<< HEAD
18
11
Ada dua sintaks untuk membuat objek ekspresi reguler.
19
-
=======
20
-
There are two syntaxes that can be used to create a regular expression object.
21
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
22
12
23
13
Syntax panjang:
24
14
25
15
```js
26
16
regexp =newRegExp("pattern", "flag");
27
17
```
28
18
29
-
<<<<<<< HEAD
30
-
...Dan yang pendek, menggunakan garis miring `"/"`:
31
-
=======
32
-
And the "short" one, using slashes `"/"`:
33
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
19
+
Dan yang "pendek", menggunakan garis miring `"/"`:
34
20
35
21
```js
36
22
regexp =/pattern/; // tanpa flag
@@ -39,19 +25,11 @@ regexp = /pattern/gmi; // dengan flag g,m dan i (untuk segera ditutup)
39
25
40
26
Garis miring `pattern:/.../` memberitahu JavaScript bahwa kita sedang membuat regular expression. Mereka memiliki peran yang sama dengan tanda petik untuk *string*.
41
27
42
-
<<<<<<< HEAD
43
28
Untuk kedua kasus `regexp` menjadi object kelas `RegExp` built-in.
44
29
45
30
Perbedaan utama antara kedua syntax ini iadalah garis miring `pattern:/.../` melarang penyisipan expresi (seperti string dengan `${...}`). Mereka benar-benar static.
46
31
47
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:
48
-
=======
49
-
In both cases `regexp` becomes an instance of the built-in `RegExp` class.
50
-
51
-
The main difference between these two syntaxes is that pattern using slashes `/.../` does not allow for expressions to be inserted (like string template literals with `${...}`). They are fully static.
52
-
53
-
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 more often used when we need to create a regexp "on the fly" from a dynamically generated string. For instance:
54
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
55
33
56
34
```js
57
35
let tag =prompt("What tag do you want to find?", "h2");
@@ -69,11 +47,7 @@ Cuma ada 6 di antaranya di JavaScript:
69
47
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
70
48
71
49
`pattern:g`
72
-
<<<<<<< HEAD
73
-
: Dengan flag ini pencarian mencari semua kecocokan, tanpanya -- hanya yang pertama.
74
-
=======
75
-
: With this flag the search looks for all matches, without it -- only the first match is returned.
76
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
50
+
: Dengan flag ini pencarian mencari semua kecocokan, tanpanya -- hanya yang pertama yang dikembalikan.
77
51
78
52
`pattern:m`
79
53
: Mode baris-ganda (dibahas di bab <info:regexp-multiline-mode>).
@@ -97,11 +71,7 @@ Skema warnanya adalah:
97
71
98
72
## Searching: str.match
99
73
100
-
<<<<<<< HEAD
101
74
Seperti yang dikatakan sebelumnya, regular expressions terintegrasi dengan metode string.
102
-
=======
103
-
As mentioned previously, regular expressions are integrated with string methods.
104
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
105
75
106
76
Metode ini `str.match(regexp)` mencari semua kecocokan `regexp` di dalam string `str`.
107
77
@@ -132,11 +102,7 @@ Ia punya 3 mode kerja:
132
102
133
103
3. Dan, akhirnya, jika tak ada kecocokan, `null`dikembalikan (tak peduli apakah ada flag `pattern:g` atau tidak).
134
104
135
-
<<<<<<<HEAD
136
105
Itu nuansa paling penting. Jika tak ada kecocokan, kita tak mendapatkan array kosong, tapi `null`. Melupakan itu bisa membawa galat, misal:
137
-
=======
138
-
This a very important nuance. If there are no matches, we don't receive an empty array, but instead receive `null`. Forgetting about that may lead to errors, e.g.:
139
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
140
106
141
107
```js run
142
108
let matches = "JavaScript".match(/HTML/); // = null
@@ -146,11 +112,7 @@ Ia punya 3 mode kerja:
146
112
}
147
113
```
148
114
149
-
<<<<<<< HEAD
150
115
Jika kita mau hasilnya selalu array, kita bisa menulisnya seperti ini:
151
-
=======
152
-
If we'd like the result to always be an array, we can write it this way:
153
-
>>>>>>>2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
154
116
155
117
```js run
156
118
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
@@ -162,11 +124,7 @@ Ia punya 3 mode kerja:
162
124
163
125
## Mengganti:str.replace
164
126
165
-
<<<<<<<HEAD
166
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).
167
-
=======
168
-
The method `str.replace(regexp, replacement)` replaces matches found using `regexp`in string `str`with`replacement` (all matches if there's flag `pattern:g`, otherwise, only the first one).
169
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
170
128
171
129
Misalnya:
172
130
@@ -206,26 +164,14 @@ let regexp = /LOVE/i;
206
164
alert( regexp.test(str) ); // true
207
165
```
208
166
209
-
<<<<<<< HEAD
210
167
Lebih lanjut di bab ini kita akan mempelajari lebih regular expression, menjumpai banyak contoh dan menemui metode lainnya.
211
-
=======
212
-
Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods.
213
-
>>>>>>> 2b5ac971c1bd8abe7b17cdcf724afd84799b6cbd
214
168
215
169
Informasi penuh tentang metode ini diberikan dalam artikel <info:regexp-methods>.
216
170
217
171
## Ringkasan
218
172
219
-
<<<<<<< HEAD
220
173
- Regular expression terdiri atas pola dan flag opsional:`pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
221
174
- Tanpa flag dan simbol spesial yang akan kita pelajari nanti, pencarian menggunakan regexp sama dengan pencarian substring.
222
175
- Metode `str.match(regexp)` mencari kecocokan: semuanya jika ada flag `pattern:g`, kalau tidak cuma yang pertama.
223
176
- Metode `str.replace(regexp, replacement)` mengganti kecocokan dengan `regexp` by `replacement`: semuanya jika ada flag `pattern:g`, selain itu cuma yang pertama.
224
177
- Metode `regexp.test(str)` mengembalikan `true` jika ada paling tidak satu kecocokan, kalau tidak `false`.
225
-
=======
226
-
- A regular expression consists of a pattern and optional flags: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
227
-
- Without flags and special symbols (that we'll study later), the search by a regexp is the same as a substring search.
228
-
- The method `str.match(regexp)` looks for matches: all of them if there's `pattern:g` flag, otherwise, only the first one.
229
-
- The method `str.replace(regexp, replacement)` replaces matches found using `regexp` with `replacement`: all of them if there's `pattern:g` flag, otherwise only the first one.
230
-
- The method `regexp.test(str)` returns `true` if there's at least one match, otherwise, it returns `false`.
0 commit comments