Skip to content

Commit 528ea5a

Browse files
authored
Merge pull request #52 from gmertk/regexp
Patterns and flags
2 parents f926425 + 38c576f commit 528ea5a

File tree

2 files changed

+127
-71
lines changed

2 files changed

+127
-71
lines changed
Lines changed: 125 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,179 @@
1-
# Patterns and flags
1+
# Kalıplar ve işaretler
22

3-
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
3+
Düzenli ifadeler, metinde arama ve değiştirme yapmak için etkili bir yöntem sağlayan kalıplardır.
44

5-
There are two syntaxes to create a regular expression object.
5+
JavaScript'te, [RegExp](mdn:js/RegExp) nesnesi aracılığıyla kullanılabilirler ve ayrıca string metotlarına entegre edilmişlerdir.
66

7-
The long syntax:
7+
## Düzenli İfadeler
8+
9+
Bir düzenli ifade ("regexp" veya sadece "reg") bir _kalıp_ ve isteğe bağlı *işaretler*den oluşur.
10+
11+
Düzenli ifade nesnesi oluşturmak için kullanılabilecek iki sözdizimi vardır.
12+
13+
Uzun olan sözdizimi:
814

915
```js
10-
regexp = new RegExp("pattern", "flags");
16+
regexp = new RegExp("kalıp", "işaretler");
1117
```
1218

13-
...And the short one, using slashes `"/"`:
19+
Ve bölme işareti "/" kullanan kısa olanı:
1420

1521
```js
16-
regexp = /pattern/; // no flags
17-
regexp = /pattern/gmi; // with flags g,m and i (to be covered soon)
22+
regexp = /kalıp/; // işaret yok
23+
regexp = /kalıp/gim; // g, m ve i işaretleriyle birlikte (yakında ele alınacak)
1824
```
1925

20-
Slashes `"/"` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
26+
Bölme işaretleri `pattern:/.../` JavaScript'e düzenli bir ifade oluşturduğumuzu söyler. Aynı tırnak işaretlerinin String oluşturduğumuzu söylediği gibi.
2127

22-
## Usage
28+
Her iki durumda da `regexp`, `RegExp` sınıfının bir örneği oluşturulmuş olur.
2329

24-
To search inside a string, we can use method [search](mdn:js/String/search).
30+
Bu ikisi arasındaki temel fark, bölme işareti `/.../` kullanan modelin kalıba expression eklenmesine izin vermemesidir (`${...}` kullanan string şablonları gibi). Tamamen statiktirler.
2531

26-
Here's an example:
32+
Bölme işaretleri, kod yazarken düzenli ifadeyi önceden bildiğimizde kullanılır -- ve bu en yaygın durumdur. `new RegExp` ise, dinamik olarak oluşturulan bir string'den anında bir regexp oluşturmamız gerektiğinde daha sık kullanılır. Örneğin:
2733

28-
```js run
29-
let str = "I love JavaScript!"; // will search here
34+
```js
35+
let tag = prompt("Hangi tag'i bulmak istiyorsun?", "h2");
3036

31-
let regexp = /love/;
32-
alert( str.search(regexp) ); // 2
37+
let regexp = new RegExp(`<${tag}>`); // yukarıdaki prompt'a "h2" cevabı verildiyse, /<h2>/ ile aynı olur.
3338
```
3439

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.
40+
## İşaretler
3641

37-
The code above is the same as:
42+
Düzenli ifadelerde aramayı etkileyen işaretler olabilir.
3843

39-
```js run
40-
let str = "I love JavaScript!"; // will search here
44+
JavaScript'te bunlardan sadece 6 tanesi var:
4145

42-
let substr = 'love';
43-
alert( str.search(substr) ); // 2
44-
```
46+
`pattern:i`
47+
: Bu işaretle arama, büyük/küçük harfe duyarlı değildir: `A` ve `a` arasında fark yoktur (aşağıdaki örneğe bakın).
4548

46-
So searching for `pattern:/love/` is the same as searching for `"love"`.
49+
`pattern:g`
50+
: Bu işaretle arama, tüm eşleşenleri arar. Eğer g işareti yoksa, sadece ilk eşleme döndürülür.
4751

48-
But that's only for now. Soon we'll create more complex regular expressions with much more searching power.
52+
`pattern:m`
53+
: Çok satırlı mod (<info:regexp-multiline-mode> bölümünde ele alınmıştır).
4954

50-
```smart header="Colors"
51-
From here on the color scheme is:
55+
`pattern:s`
56+
: Yeni satır karakteri `\n` ile eşleşmek için noktaya `pattern:.` izin veren “dotall” modunu etkinleştirir. (<info:regexp-character-classes> bölümünde ele alınmıştır).
57+
58+
`pattern:u`
59+
: Tam unicode desteğini etkinleştirir. Bu işaret, vekil çiftlerin doğru işlenmesini sağlar. Bununla ilgili daha fazla bilgi <info:regexp-unicode> bölümünde.
60+
61+
`pattern:y`
62+
: “Yapışkan” mod: metindeki tam pozisyonu arar (<info:regexp-sticky> bölümünde ele alınmıştır)
63+
64+
```smart header="Renkler"
65+
Buradan itibaren renk şeması şu şekilde olacak:
5266
5367
- regexp -- `pattern:red`
54-
- string (where we search) -- `subject:blue`
55-
- result -- `match:green`
68+
- string (aradığımız yer) -- `subject:blue`
69+
- sonuç -- `match:green`
5670
```
5771

72+
## Arama: str.match
5873

59-
````smart header="When to use `new RegExp`?"
60-
Normally we use the short syntax `/.../`. But it does not support variable insertions `${...}`.
74+
Daha önce de belirtildiği gibi, düzenli ifadeler string metotlarına entegre edilmiştir.
6175

62-
On the other hand, `new RegExp` allows to construct a pattern dynamically from a string, so it's more flexible.
76+
`str.match(regexp)` metodu `regexp` ile `str` stringindeki tüm eşleşmeleri bulur.
6377

64-
Here's an example of a dynamically generated regexp:
78+
3 çalışma modu vardır:
6579

66-
```js run
67-
let tag = prompt("Which tag you want to search?", "h2");
68-
let regexp = new RegExp(`<${tag}>`);
80+
1. Düzenli ifadede `pattern:g` işareti varsa, tüm eşleşmelerden oluşan bir dizi döndürür:
6981

70-
// finds <h2> by default
71-
alert( "<h1> <h2> <h3>".search(regexp));
72-
```
73-
````
82+
```js run
83+
let str = "We will, we will rock you";
84+
85+
alert( str.match(/we/gi) ); // We,we (eşleşen 2 string'den oluşan bir dizi)
86+
```
87+
Hem `match:We` hem de `match:we`'nin bulunduğuna dikkat edin, çünkü `pattern:i` işareti aramayı büyük/küçük harfe duyarsız hale getirir.
88+
89+
2. Eğer `pattern:g` işareti yoksa, yalnızca ilk eşleşmeyi döndürür. Dönen ifade bir dizi formundadır. İlk eşleşme dizininin `0` indeksinde yer alır ve dizinin bazı ek ayrıntılar sağlayan property'leri vardır:
90+
```js run
91+
let str = "We will, we will rock you";
7492
93+
let result = str.match(/we/i); // g işareti olmadan
7594
76-
## Flags
95+
alert( result[0] ); // We (ilk eşleşme)
96+
alert( result.length ); // 1
7797
78-
Regular expressions may have flags that affect the search.
98+
// Details:
99+
alert( result.index ); // 0 (eşlemenin string'deki pozisyonu)
100+
alert( result.input ); // We will, we will rock you (kaynak string)
101+
```
102+
Düzenli ifadenin bir kısmı parantez içine alınmışsa dizinin `0` dışında başka indeksleri de olabilir. Bunu, <info:regexp-groups> bölümünde ele alacağız.
79103

80-
There are only 6 of them in JavaScript:
104+
3. Son olarak, eşleşme yoksa `null` döndürülür (`pattern:g` işareti olup olmadığı önemli değildir).
81105

82-
`i`
83-
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
106+
Bu çok önemli bir nüans. Eşleşme yoksa boş bir dizi almıyoruz, bunun yerine `null` alıyoruz. Bunu unutmak hatalara neden olabilir, örneğin:
84107

85-
`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).
108+
```js run
109+
let matches = "JavaScript".match(/HTML/); // = null
87110
88-
`m`
89-
: Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
111+
if (!matches.length) { // Error: Cannot read property 'length' of null
112+
alert("Yukardaki satırda hata var");
113+
}
114+
```
90115

91-
`s`
92-
: "Dotall" mode, allows `.` to match newlines (covered in the chapter <info:regexp-character-classes>).
116+
Sonucun her zaman bir dizi olmasını istiyorsak, bunu şu şekilde yazabiliriz:
93117

94-
`u`
95-
: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
118+
```js run
119+
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
96120
97-
`y`
98-
: Sticky mode (covered in the chapter <info:regexp-sticky>)
121+
if (!matches.length) {
122+
alert("Eşleşme yok"); // şimdi çalışıyor
123+
}
124+
```
99125

100-
We'll cover all these flags further in the tutorial.
126+
## Yer değiştirme: str.replace
101127

102-
For now, the simplest flag is `i`, here's an example:
128+
`str.replace(regexp, yeni_str)` metodu, string `str`'de `regexp` kullanılarak bulunan eşleşmeleri `yeni_str` ile değiştirir (`pattern:g` işareti varsa tüm eşleşmeler, aksi takdirde yalnızca ilk olanı).
129+
130+
Örneğin:
103131
104132
```js run
105-
let str = "I love JavaScript!";
133+
// no flag g
134+
alert( "We will, we will".replace(/we/i, "I") ); // I will, we will
135+
136+
// with flag g
137+
alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will
138+
```
139+
140+
İkinci argüman, `yeni_str` bulunan eşlemenin yerine geçen string'dir. Eşleşmenin parçalarını eklemek için özel karakter kombinasyonları kullanabiliriz:
106141

107-
alert( str.search(/LOVE/i) ); // 2 (found lowercased)
142+
| Semboller | Değiştirilen yeni_str'de yapılan eylem |
143+
|--------|--------|
144+
|`$&`|tüm eşleşmeyi ekler|
145+
|<code>$&#096;</code>|string'in eşleşmeden önce gelen kısmını ekler|
146+
|`$'`|string'in eşleşmeden sonra gelen kısmını ekler|
147+
|`$n`|`n` 1-2 basamaklı bir sayıysa, n. parantezlerin içeriğini ekler, bununla ilgili daha fazla bilgi <info:regexp-groups> bölümünde|
148+
|`$<isim>`|`isim` adı verilen parantezlerin içeriğini ekler, daha fazlası <info:regexp-groups> bölümünde|
149+
|`$$`|`$` karakterini ekler|
150+
151+
`pattern:$&` ile bir örnek::
152+
153+
```js run
154+
alert( "I love HTML".replace(/HTML/, "$& and JavaScript") ); // I love HTML and JavaScript
155+
```
156+
157+
## Test: regexp.test
158+
159+
`regexp.test(str)` metodu en az bir eşleşme arar, eşleşme bulunursa `true` değerini döndürür, aksi takdirde `false` değerini döndürür.
160+
161+
162+
```js run
163+
let str = "I love JavaScript";
164+
let regexp = /LOVE/i;
108165
109-
alert( str.search(/LOVE/) ); // -1 (nothing found without 'i' flag)
166+
alert( regexp.test(str) ); // true
110167
```
111168
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.
169+
Bu bölümün ilerleyen kısımlarında düzenli ifadeler üzerinde daha fazla çalışacağız, daha fazla örnek üzerinde duracağız ve diğer metotlarla da karşılaşacağız.
113170
171+
Metotlar hakkında tam bilgi <info:regexp-methods> makalesinde verilmiştir.
114172
115-
## Summary
173+
## Özet
116174
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.
175+
- Düzenli ifade bir kalıp ve isteğe bağlı işaretlerden oluşur: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
176+
- İşaretler ve özel semboller olmadan (daha sonra inceleyeceğiz) düzenli ifadelerle arama bir substring aramasıyla aynıdır.
177+
- `str.match(regexp)` metodu eşleşmeleri arar: `pattern:g` işareti varsa tümü, aksi takdirde yalnızca birincisini döner.
178+
- `str.replace(regexp, yeni_str)` metodu, `regexp` kullanılarak bulunan eşleşmeleri `yeni_str` ile değiştirir: `pattern:g` işareti varsa tümünü, aksi takdirde yalnızca ilkini değiştirir.
179+
- `regexp.test(str)` metodu en az bir eşleşme varsa `true` değerini döndürür, aksi takdirde `false` değerini döndürür.

9-regular-expressions/index.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# Regular expressions
1+
# Düzenli İfadeler
22

3-
Regular expressions is a powerful way of doing search and replace in strings.
4-
5-
In JavaScript regular expressions are implemented using objects of a built-in `RegExp` class and integrated with strings.
6-
7-
Please note that regular expressions vary between programming languages. In this tutorial we concentrate on JavaScript. Of course there's a lot in common, but they are a somewhat different in Perl, Ruby, PHP etc.
3+
Düzenli ifadeler, dizelerde arama ve değiştirme yapmanın etkili bir yoludur.

0 commit comments

Comments
 (0)