|
1 | | -# Anchors: string start ^ and end $ |
| 1 | +# Anchors: mulai _string_ ^ dan akhiran $ |
2 | 2 |
|
3 | | -The caret `pattern:^` and dollar `pattern:$` characters have special meaning in a regexp. They are called "anchors". |
| 3 | +Tanda karakter _caret_ `^` dan tanda _dollar_ `$` memiliki arti spesial pada _regexp_. Tanda-tanda tersebut dipanggil dengan nama _"Ancors"_. |
4 | 4 |
|
5 | | -The caret `pattern:^` matches at the beginning of the text, and the dollar `pattern:$` -- at the end. |
| 5 | +Tanda _caret_ `^` membandingkan awalan dari _text_, dan tanda _dollar_ `$` -- membandingkan akhirannya. |
6 | 6 |
|
7 | | -For instance, let's test if the text starts with `Mary`: |
| 7 | +Contoh, Kita coba jika _text_ nya memiliki awalan `Mary`: |
8 | 8 |
|
9 | 9 | ```js run |
10 | 10 | let str1 = "Mary had a little lamb"; |
11 | | -alert( /^Mary/.test(str1) ); // true |
| 11 | +alert(/^Mary/.test(str1)); // true |
12 | 12 | ``` |
13 | 13 |
|
14 | | -The pattern `pattern:^Mary` means: "string start and then Mary". |
| 14 | +Pola `^Mary` berarti: "_text_ nya dimulai dengan Mary" |
15 | 15 |
|
16 | | -Similar to this, we can test if the string ends with `snow` using `pattern:snow$`: |
| 16 | +Sama seperti ini, kita bisa mencoba jika _text_ nya berakhiran dengan `snow` menggunakan `snow$`: |
17 | 17 |
|
18 | 18 | ```js run |
19 | 19 | let str1 = "it's fleece was white as snow"; |
20 | | -alert( /snow$/.test(str1) ); // true |
| 20 | +alert(/snow$/.test(str1)); // true |
21 | 21 | ``` |
22 | 22 |
|
23 | | -In these particular cases we could use string methods `startsWith/endsWith` instead. Regular expressions should be used for more complex tests. |
| 23 | +Dalam beberapa kasus tertentu kita bisa menggunakan metode _string_ `startsWith/endsWith`. _Regular Expression_ hanya digunakan untuk _test_ yang lebih kompleks. |
24 | 24 |
|
25 | | -## Testing for a full match |
| 25 | +## Membandingkan keseluruhannya |
26 | 26 |
|
27 | | -Both anchors together `pattern:^...$` are often used to test whether or not a string fully matches the pattern. For instance, to check if the user input is in the right format. |
| 27 | +Kedua _achors_ `^...%` sering digunakan untuk menguji apakah sebuah _string_ sama keseluruhannya secara pola atau tidak. Contohnya, untuk memeriksa apakah _input_ dari _user_ menggunakan format yang benar. |
28 | 28 |
|
29 | | -Let's check whether or not a string is a time in `12:34` format. That is: two digits, then a colon, and then another two digits. |
| 29 | +Kita coba apakah `12:34` adalah format _string_ atau bukan. Menggunakan: dua angka, lalu titik dua, lalu dua angka lagi. |
30 | 30 |
|
31 | | -In regular expressions language that's `pattern:\d\d:\d\d`: |
| 31 | +Didalam bahasa _regular expressions_ itu adalah `pola:\d\d:\d\d`: |
32 | 32 |
|
33 | 33 | ```js run |
34 | 34 | let goodInput = "12:34"; |
35 | 35 | let badInput = "12:345"; |
36 | 36 |
|
37 | 37 | let regexp = /^\d\d:\d\d$/; |
38 | | -alert( regexp.test(goodInput) ); // true |
39 | | -alert( regexp.test(badInput) ); // false |
| 38 | +alert(regexp.test(goodInput)); // true |
| 39 | +alert(regexp.test(badInput)); // false |
40 | 40 | ``` |
41 | 41 |
|
42 | | -Here the match for `pattern:\d\d:\d\d` must start exactly after the beginning of the text `pattern:^`, and the end `pattern:$` must immediately follow. |
| 42 | +Disini yang cocok untuk `pola:\d\d:\d\d` harus dimulai dengan `^` dan diakhiri dengan `$` yang mana harus cocok. |
43 | 43 |
|
44 | | -The whole string must be exactly in this format. If there's any deviation or an extra character, the result is `false`. |
| 44 | +Seluruh _string_ nya harus menggunakan format yang sama. Jika ada yang berbeda atau ada karakter lebih, maka hasilnya akan menjadi `false`. |
45 | 45 |
|
46 | | -Anchors behave differently if flag `pattern:m` is present. We'll see that in the next article. |
| 46 | +_Anchors_ bertindak berbeda jika tanda `pola:m` ada. Kita akan mempelajarinya di bab selanjutnya. |
47 | 47 |
|
48 | | -```smart header="Anchors have \"zero width\"" |
49 | | -Anchors `pattern:^` and `pattern:$` are tests. They have zero width. |
| 48 | +```smart header="Anchors memiliki "zero width"" |
| 49 | +Anchors `pola:^` dan `pola:$` adalah test. Keduanya memiliki "zero width". |
50 | 50 |
|
51 | | -In other words, they do not match a character, but rather force the regexp engine to check the condition (text start/end). |
| 51 | +Dengan kata lain, keduanya tidak mencocokan karakter, akan tetapi memaksa regexp untuk memeriksa kondisi (awal text/akhir text) |
52 | 52 | ``` |
0 commit comments