Skip to content

Commit 20fd604

Browse files
authored
Merge pull request #26 from iamfaizalandyka/regular-expressions
Regular expressions: Patterns and flags
2 parents 73a61f1 + 73aa4f4 commit 20fd604

File tree

1 file changed

+47
-48
lines changed
  • 9-regular-expressions/01-regexp-introduction

1 file changed

+47
-48
lines changed
Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,118 @@
1-
# Patterns and flags
1+
# Pattern dan flag
22

3-
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
3+
Ekspresi reguler ("*regexp*", atau hanya "*reg*") terdiri dari *pattern* dan opsional *flag*.
44

5-
There are two syntaxes to create a regular expression object.
5+
Ada dua sintaks untuk membuat objek ekspresi reguler.
66

7-
The long syntax:
7+
Sintaks yang panjang:
88

99
```js
10-
regexp = new RegExp("pattern", "flags");
10+
regexp = new RegExp("pattern", "flag");
1111
```
1212

13-
...And the short one, using slashes `"/"`:
13+
...Dan yang pendek, menggunakan garis miring `"/"`:
1414

1515
```js
16-
regexp = /pattern/; // no flags
17-
regexp = /pattern/gmi; // with flags g,m and i (to be covered soon)
16+
regexp = /pattern/; // tanpa flag
17+
regexp = /pattern/gmi; // dengan flag g,m dan i (untuk segera ditutup)
1818
```
1919

20-
Slashes `"/"` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
20+
Garing miring `"/"` akan memberitahu JavaScript bahwa kita sedang membuat ekspresi reguler. Mereka memiliki peran yang sama seperti *quotes* untuk *strings*.
2121

22-
## Usage
22+
## Penggunaan
2323

24-
To search inside a string, we can use method [search](mdn:js/String/search).
24+
Untuk mencari di dalam sebuah string, kita dapat menggunakan metode [*search*](mdn:js/String/search).
2525

26-
Here's an example:
26+
Berikut contohnya:
2727

2828
```js run
29-
let str = "I love JavaScript!"; // will search here
29+
let str = "I love JavaScript!"; // akan mencari disini
3030

3131
let regexp = /love/;
3232
alert( str.search(regexp) ); // 2
3333
```
3434

35-
The `str.search` method looks for the pattern `pattern:/love/` and returns the position inside the string. As we might guess, `pattern:/love/` is the simplest possible pattern. What it does is a simple substring search.
35+
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.
3636

37-
The code above is the same as:
37+
Code di atas sama seperti berikut:
3838

3939
```js run
40-
let str = "I love JavaScript!"; // will search here
40+
let str = "I love JavaScript!"; // akan mencari disini
4141

4242
let substr = 'love';
4343
alert( str.search(substr) ); // 2
4444
```
4545

46-
So searching for `pattern:/love/` is the same as searching for `"love"`.
46+
Jadi, mencari `pattern:/love/` sama seperti mencari `"love"`.
4747

48-
But that's only for now. Soon we'll create more complex regular expressions with much more searching power.
48+
Tapi itu hanya untuk saat ini. Kita akan membuat ekspresi reguler yang lebih kompleks dengan kekuatan *search* yang jauh lebih banyak.
4949

50-
```smart header="Colors"
51-
From here on the color scheme is:
50+
```smart header="Warna"
51+
Skema warnanya adalah:
5252
53-
- regexp -- `pattern:red`
54-
- string (where we search) -- `subject:blue`
55-
- result -- `match:green`
53+
- regexp -- `pattern:merah`
54+
- string (tempat kita mencari) -- `subject:biru`
55+
- result -- `match:hijau`
5656
```
5757

5858

59-
````smart header="When to use `new RegExp`?"
60-
Normally we use the short syntax `/.../`. But it does not support variable insertions `${...}`.
59+
````smart header="Kapan harus menggunakan `new RegExp`?"
60+
Biasanya kita menggunakan sintaks pendek `/.../`. Tetapi itu tidak mendukung penyisipan variabel menggunakan sintaks `${...}`.
6161

62-
On the other hand, `new RegExp` allows to construct a pattern dynamically from a string, so it's more flexible.
62+
Di sisi lain, `new RegExp` memungkinkan untuk membangun pola secara dinamis dari string, jadi lebih fleksibel.
6363

64-
Here's an example of a dynamically generated regexp:
64+
Berikut contoh dari *regexp* yang dihasilkan secara dinamis:
6565

6666
```js run
67-
let tag = prompt("Which tag you want to search?", "h2");
67+
let tag = prompt("Tag mana yang ingin Kamu cari?", "h2");
6868
let regexp = new RegExp(`<${tag}>`);
6969

70-
// finds <h2> by default
70+
// mencari <h2> secara default
7171
alert( "<h1> <h2> <h3>".search(regexp));
7272
```
7373
````
7474
7575
76-
## Flags
76+
## Flag
7777
78-
Regular expressions may have flags that affect the search.
78+
Ekspresi reguler mungkin memiliki *flag* yang dapat mempengaruhi *search*.
7979
80-
There are only 6 of them in JavaScript:
80+
Mereka hanya ada 6 di Javascript:
8181
8282
`i`
83-
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
83+
: Dengan *flag* ini, *search* tidak peka terhadap huruf besar maupun kecil: tidak ada perbedaan antara `A` and `a` (lihat contohnya di bawah ini).
8484
8585
`g`
86-
: With this flag the search looks for all matches, without it -- only the first one (we'll see uses in the next chapter).
86+
: 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).
8787
8888
`m`
89-
: Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
89+
: Mode *Multiline* (tercantum di dalam bab <info:regexp-multiline-mode>).
9090
9191
`s`
92-
: "Dotall" mode, allows `.` to match newlines (covered in the chapter <info:regexp-character-classes>).
92+
: Mode *Dotall*, memungkinkan `.` untuk cocok dengan baris baru (tecantum di dalam bab <info:regexp-character-classes>).
9393
9494
`u`
95-
: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
95+
: 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>.
9696
9797
`y`
98-
: Sticky mode (covered in the chapter <info:regexp-sticky>)
98+
: Mode *Sticky* (tercantum di dalam bab <info:regexp-sticky>)
9999
100-
We'll cover all these flags further in the tutorial.
100+
Kita akan membahas semua *flag* tersebut lebih lanjut di dalam tutorial.
101101
102-
For now, the simplest flag is `i`, here's an example:
102+
Untuk sekarang, *flag* paling sederhana adalah `i`, berikut contohnya:
103103
104104
```js run
105105
let str = "I love JavaScript!";
106106
107-
alert( str.search(/LOVE/i) ); // 2 (found lowercased)
108-
109-
alert( str.search(/LOVE/) ); // -1 (nothing found without 'i' flag)
107+
alert( str.search(/LOVE/i) ); // 2 (lowercased ditemukan)
108+
alert( str.search(/LOVE/) ); // -1 (tidak ada yang ditemukan tanpa flag 'i')
110109
```
111110
112-
So the `i` flag already makes regular expressions more powerful than a simple substring search. But there's so much more. We'll cover other flags and features in the next chapters.
111+
*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.
113112
114113
115-
## Summary
114+
## Ringkasan
116115
117-
- A regular expression consists of a pattern and optional flags: `g`, `i`, `m`, `u`, `s`, `y`.
118-
- Without flags and special symbols that we'll study later, the search by a regexp is the same as a substring search.
119-
- The method `str.search(regexp)` returns the index where the match is found or `-1` if there's no match. In the next chapter we'll see other methods.
116+
- Ekspresi reguler terdiri dari *pattern* dan opsional *flag*: `g`, `i`, `m`, `u`, `s`, `y`.
117+
- Tanpa *flag* dan simbol khusus yang akan kita pelajari nanti, *search* dengan *regexp* sama seperti *search* dengan substring.
118+
- Metode `str.search(regexp)` mengembalikan *index* yang cocok atau `-1` jika tidak ada yang cocok. Dalam bab selanjutnya kita akan melihat metode lain.

0 commit comments

Comments
 (0)