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
Consider a practical task -- we have a phone number like `"+7(903)-123-45-67"`, and we need to turn it into pure numbers: `79031234567`.
3
+
Bayangkan sebuah tugas praktik -- kita memiliki sebuah nomor telepon `"+7(903)-123-45-67"`, dan kita harus mengubah nomor telepon tersebut menjadi angka murni: `79031234567`.
4
4
5
-
To do so, we can find and remove anything that's not a number. Character classes can help with that.
5
+
Untuk melakukan hal itu, kita harus menemukan apapun yang bukan merupakan angka. Kelas karakter dapat membantu membereskan hal tersebut.
6
6
7
-
A *character class* is a special notation that matches any symbol from a certain set.
7
+
Sebuah *Kelas Karakter* adalah sebuah notasi yang spesial yang membandingkan simbol apapun dengan aturan tertentu.
8
8
9
-
For the start, let's explore the "digit" class. It's written as `pattern:\d`and corresponds to "any single digit".
9
+
Untuk memulai, ayo kita *explore* kelas "angka" (digit). Kelas "angka" ditulis seperti `pattern:\d`dan dapat disamakan dengan "setiap angka yang ada".
10
10
11
-
For instance, let's find the first digit in the phone number:
11
+
Untuk contoh, kita akan mencoba mencari angka pertama pada nomor telepon:
12
12
13
13
```js run
14
14
let str ="+7(903)-123-45-67";
@@ -18,186 +18,187 @@ let regexp = /\d/;
18
18
alert( str.match(regexp) ); // 7
19
19
```
20
20
21
-
Without the flag `pattern:g`, the regular expression only looks for the first match, that is the first digit`pattern:\d`.
21
+
Tanpa tanda `pattern:g`, ekspresi regularnya hanya akan mencari satu kecocokan, yaitu angka pertama`pattern:\d`.
22
22
23
-
Let's add the `pattern:g`flag to find all digits:
23
+
Coba kita tambahkan tanda `pattern:g`untuk mencari seluruh angka:
24
24
25
25
```js run
26
26
let str ="+7(903)-123-45-67";
27
27
28
28
let regexp =/\d/g;
29
29
30
-
alert( str.match(regexp) ); // array of matches: 7,9,0,3,1,2,3,4,5,6,7
That was a character class for digits. There are other character classes as well.
36
+
Itu adalah kelas karakter untuk angka. Tentu saja ada karakter kelas lainnya.
37
37
38
-
Most used are:
38
+
Yang paling banyak digunakan adalah:
39
39
40
-
`pattern:\d` ("d" is from "digit")
41
-
: A digit: a character from`0`to`9`.
40
+
`pattern:\d` ("d" untuk "digit" / "angka")
41
+
: Angka: adalah karakter dari`0`sampai`9`.
42
42
43
-
`pattern:\s` ("s" is from "space")
44
-
: A space symbol: includes spaces, tabs `\t`, newlines `\n`and few other rare characters, such as `\v`, `\f`and`\r`.
43
+
`pattern:\s` ("s" untuk "space" / "spasi" / untuk mencari *whitespace*)
44
+
: Simbol *space* / spasi: termasuk spasi, *tabs*`\t`, garis baru `\n`dan beberapa karakter lainnya yang jarang digunakan, seperti `\v`, `\f`dan`\r`.
45
45
46
-
`pattern:\w` ("w" is from "word")
47
-
: A "wordly" character: either a letter of Latin alphabet or a digit or an underscore `_`. Non-Latin letters (like cyrillic or hindi) do not belong to `pattern:\w`.
46
+
`pattern:\w` ("w" untuk "word" / "karakter kata")
47
+
: "karakter kata": antara sebuah huruf dari alfabet latin atau sebuah angka atau sebuah *underscore*`_` (a-z, A-Z, 0-9). `pattern:\w` tidak bisa digunakan untuk huruf yang bukan huruf latin (seperti *cyrillic* atau *hindi*).
48
48
49
-
For instance, `pattern:\d\s\w`means a "digit" followed by a "space character" followed by a "wordly character", such as`match:1 a`.
49
+
Contohnya, `pattern:\d\s\w`menandakan sebuah "digit" yang diikuti dengan "karakter *space*" diikuti dengan "karakter kata", seperti`match:1 a`.
50
50
51
-
**A regexp may contain both regular symbols and character classes.**
51
+
**Regular ekspresi mungkin mengandung simbol dan kelas karakter.**
52
52
53
-
For instance, `pattern:CSS\d`matches a string `match:CSS`with a digit after it:
53
+
Contoh, `pattern:CSS\d`cocok dengan *string*`match:CSS`dengan angka setelahnya:
54
54
55
55
```js run
56
-
let str ="Is there CSS4?";
56
+
let str ="Apakah CSS4 telah rilis?";
57
57
let regexp =/CSS\d/
58
58
59
59
alert( str.match(regexp) ); // CSS4
60
60
```
61
61
62
-
Also we can use many character classes:
62
+
Dan juga kita bisa menggunakan banyak kelas karakter:
63
63
64
64
```js run
65
65
alert( "I love HTML5!".match(/\s\w\w\w\w\d/) ); // ' HTML5'
66
66
```
67
67
68
-
The match (each regexp character class has the corresponding result character):
68
+
Kecocokannya (setiap karakter kelas regexp yang menghasilkan hasil yang sesuai):
69
69
70
70

71
71
72
-
## Inverse classes
72
+
## Kelas kebalikan (Inverse Classes)
73
73
74
-
For every character class there exists an "inverse class", denoted with the same letter, but uppercased.
74
+
Untuk setiap kelas karakter yang ada terdapat sebuah "kelas kebalikan", ditunjukan dengan huruf yang sama, tapi dengan huruf besar / kapital.
75
75
76
-
The "inverse" means that it matches all other characters, for instance:
76
+
"Kebalikan" berarti kelas tersebut cocok dengan seluruh karakter lainnya, contoh:
77
77
78
78
`pattern:\D`
79
-
: Non-digit: any character except`pattern:\d`, for instance a letter.
79
+
: Bukan angka: karakter apapun kecuali`pattern:\d`, contoh huruf.
80
80
81
81
`pattern:\S`
82
-
: Non-space: any character except`pattern:\s`, for instance a letter.
82
+
: Bukan spasi: karakter apapun kecuali`pattern:\s`, contoh huruf.
83
83
84
84
`pattern:\W`
85
-
: Non-wordly character: anything but`pattern:\w`, e.g a non-latin letter or a space.
85
+
: Bukan karakter kata: apapun kecuali`pattern:\w`, contoh huruf yang bukan latin atau sebuah spasi.
86
86
87
-
In the beginning of the chapter we saw how to make a number-only phone number from a string like`subject:+7(903)-123-45-67`: find all digits and join them.
87
+
Pada awal bab kita melihat bagaimana cara membuat nomor telepon yang hanya angka dari string seperti`subject:+7(903)-123-45-67`: cari seluruh angka dan gabungkan.
An alternative, shorter way is to find non-digits `pattern:\D` and remove them from the string:
95
+
Alternatif, cara pendeknya adalah dengan mencari karakter yang bukan angka dan hilangkan dari stringnya:
96
96
97
97
```js run
98
98
let str ="+7(903)-123-45-67";
99
99
100
100
alert( str.replace(/\D/g, "") ); // 79031234567
101
101
```
102
102
103
-
## A dot is "any character"
103
+
## Sebuah titik adalah "karakter apapun"
104
104
105
-
A dot`pattern:.`is a special character class that matches "any character except a newline".
105
+
Sebuah titik`pattern:.`adalah sebuah kelas karakter spesial yang cocok "dengan karakter apapun kecuali garis baru".
106
106
107
-
For instance:
107
+
Contoh:
108
108
109
109
```js run
110
110
alert( "Z".match(/./) ); // Z
111
111
```
112
112
113
-
Or in the middle of a regexp:
113
+
Atau ditengah dari sebuah ekspresi regular:
114
114
115
115
```js run
116
116
let regexp =/CS.4/;
117
117
118
118
alert( "CSS4".match(regexp) ); // CSS4
119
119
alert( "CS-4".match(regexp) ); // CS-4
120
-
alert( "CS 4".match(regexp) ); // CS 4 (space is also a character)
120
+
alert( "CS 4".match(regexp) ); // CS 4 (spasi juga merupakan karakter)
121
121
```
122
122
123
-
Please note that a dot means "any character", but not the "absence of a character". There must be a character to match it:
123
+
Ingatlah bahwa titik berarti "karakter apapun", tapi bukan sebuah "ketiadaan karakter". Harusnya ada karakter yang mirip dengannya:
124
124
125
125
```js run
126
-
alert( "CS4".match(/CS.4/) ); // null, no match because there's no character for the dot
126
+
alert( "CS4".match(/CS.4/) ); // null, tidak ada yang cocok karena tidak terdapat karakter untuk titiknya.
127
127
```
128
128
129
-
### Dot as literally any character with "s" flag
129
+
### Titik secara literal adalah karakter apapun dengan tanda "s"
130
130
131
-
By default, a dot doesn't match the newline character`\n`.
131
+
Pada dasarnya, sebuah titik tidak cocok dengan karakter garis baru`\n`.
132
132
133
-
For instance, the regexp`pattern:A.B`matches `match:A`, and then`match:B`with any character between them, except a newline`\n`:
133
+
Contoh, ekspresi regular`pattern:A.B`cocok dengan `match:A`, dan lalu`match:B`dengan karakter apapun diantaranya, kecuali garis baru`\n`:
134
134
135
135
```js run
136
-
alert( "A\nB".match(/A.B/) ); // null (no match)
136
+
alert( "A\nB".match(/A.B/) ); // null (tidak ada yang cocok)
137
137
```
138
138
139
-
There are many situations when we'd like a dot to mean literally "any character", newline included.
139
+
Terdapat beberapa situasi ketika kita ingin memperlakukan titik sebagai "karakter apapun", garis baru termasuk.
140
140
141
-
That's what flag `pattern:s` does. If a regexp has it, then a dot`pattern:.`matches literally any character:
141
+
Itulah yang dilakukan oleh `pattern:s`. Jika sebuah ekspresi regular memilikinya, maka sebuah titik`pattern:.`secara literal cocok dengan karakter apapun:
142
142
143
143
```js run
144
-
alert( "A\nB".match(/A.B/s) ); // A\nB (match!)
144
+
alert( "A\nB".match(/A.B/s) ); // A\nB (cocok!)
145
145
```
146
146
147
-
````warn header="Not supported in IE"
148
-
The `pattern:s` flag is not supported in IE.
147
+
````warn header="Tidak didukung IE"
148
+
Tanda `pattern:s` tidak didukung pada IE.
149
149
150
-
Luckily, there's an alternative, that works everywhere. We can use a regexp like `pattern:[\s\S]` to match "any character" (this pattern will be covered in the article <info:regexp-character-sets-and-ranges>).
150
+
Beruntungnya, masih terdapat alternatif untuk hal itu yang mana dapat berjalan dimanapun. Kita bisa menggunakan *regexp* seperti `pattern:[\s\S]` untuk dicocokan dengan "karakter apapun" (pola ini akan dijelaskan pada bab <info:regexp-character-sets-and-ranges>).
The pattern `pattern:[\s\S]` literally says: "a space character OR not a space character". In other words, "anything". We could use another pair of complementary classes, such as `pattern:[\d\D]`, that doesn't matter. Or even the `pattern:[^]` -- as it means match any character except nothing.
156
+
Pola `pattern:[\s\S]` bisa dikatakan: "sebuah karakter spasi ATAU bukan sebuah karakter spasi". Dengan kata lain, "apapun". Kita bisa menggunakan kelas komplemen pasangan lainnya, seperti `pattern:[\d\D]`, itu tidak penting. Atau bahkan `pattern:[^]` -- sama seperti mencocokan karakter apapun kecuali tidak ada.
157
157
158
-
Also we can use this trick if we want both kind of "dots" in the same pattern: the actual dot `pattern:.` behaving the regular way ("not including a newline"), and also a way to match "any character" with `pattern:[\s\S]` or alike.
158
+
Kita juga bisa menggunakan trik ini jika kita ingin dua bentuk "titik" didalam pola yang sama: titik yang sebenarnya `pattern:.` bertingkah seperti biasa ("tidak termasuk garis baru"), dan juga sebuah cara untuk mencocokan "karakter apapun" dengan `pattern:[\s\S]` atau sejenisnya.
159
159
````
160
160
161
-
````warn header="Pay attention to spaces"
162
-
Usually we pay little attention to spaces. For us strings `subject:1-5` and `subject:1 - 5` are nearly identical.
161
+
````warn header="Perhatikan spasinya"
162
+
Biasanya kita hanya memberikan sedikit perhatian pada spasi. Untuk kita string `subject:1-5` dan `subject:1 - 5` adalah hal yang hampir mirip.
163
163
164
-
But if a regexp doesn't take spaces into account, it may fail to work.
164
+
Tapi jika sebuah ekspresi regular tidak memperhatikan spasinya, maka pencocokannya akan gagal.
165
165
166
-
Let's try to find digits separated by a hyphen:
166
+
Kita coba untuk mencari angka yang dipisahkan dengan tanda penghubung:
167
167
168
168
```js run
169
-
alert( "1 - 5".match(/\d-\d/) ); // null, no match!
169
+
alert( "1 - 5".match(/\d-\d/) ); // null, tidak cocok!
170
170
```
171
171
172
-
Let's fix it adding spaces into the regexp `pattern:\d - \d`:
172
+
Sekarang kita benarkan dengan menambahkan spasi pada regexp-nya `pattern:\d - \d`:
173
173
174
174
```js run
175
-
alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, now it works
176
-
// or we can use \s class:
177
-
alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, also works
175
+
alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, sekarang pencocokannya bekerja
176
+
// atau kita bisa menggunakan kelas \s:
177
+
alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, juga bekerja
178
178
```
179
179
180
-
**A space is a character. Equal in importance with any other character.**
180
+
**Sebuah spasi adalah sebuah karakter. Sama pentingnya dengan karakter lainnya**
181
181
182
-
We can't add or remove spaces from a regular expression and expect it to work the same.
182
+
Kita tidak bisa menambah atau menghilangkan spasi dari ekspresi regular dan berharap pencocokanya akan berjalan sama.
183
183
184
-
In other words, in a regular expression all characters matter, spaces too.
184
+
Dengan kata lain, didalam ekspresi regular seluruh karakter bersifat penting, spasi pun sama.
185
185
````
186
186
187
-
## Summary
187
+
## Ringkasan
188
188
189
-
There exist following character classes:
189
+
Terdapat karakter kelas:
190
190
191
-
-`pattern:\d` -- digits.
192
-
-`pattern:\D` -- non-digits.
193
-
-`pattern:\s` -- space symbols, tabs, newlines.
194
-
-`pattern:\S` -- all but`pattern:\s`.
195
-
-`pattern:\w` -- Latin letters, digits, underscore `'_'`.
196
-
-`pattern:\W` -- all but`pattern:\w`.
197
-
-`pattern:.` -- any character if with the regexp `'s'` flag, otherwise any except a newline`\n`.
191
+
-`pattern:\d` -- digits / Angka.
192
+
-`pattern:\D` -- non-digits / Bukan angka.
193
+
-`pattern:\s` -- simbol spasi, tabs, garis baru.
194
+
-`pattern:\S` -- semuanya kecuali`pattern:\s`.
195
+
-`pattern:\w` -- huruf latin, angka, *underscore*`'_'`.
196
+
-`pattern:\W` -- semuanya kecuali`pattern:\w`.
197
+
-`pattern:.` -- karakter apapun jika dengan regexp tanda `'s'`, sebaliknya semuanya kecuali sebuah garis baru`\n`.
198
198
199
-
...But that's not all!
199
+
...Tapi ini belum semuanya!
200
200
201
-
Unicode encoding, used by JavaScript for strings, provides many properties for characters, like: which language the letter belongs to (if it's a letter), is it a punctuation sign, etc.
201
+
*Unicode encoding*, digunakan oleh Javascript untuk *string*, menyediakan berbagai properti untuk karakter, seperti: bahasa mana yang cocok dengan surat ini (jika sebuah surat), tanda bacanya, dll.
202
202
203
203
We can search by these properties as well. That requires flag `pattern:u`, covered in the next article.
204
+
Tentu saja kita bisa mencari dengan propertinya. Yang mana membutuhkan tanda `pattern:u`, yang akan dibahas didalam bab selanjutnya.
0 commit comments