diff --git a/9-regular-expressions/01-regexp-introduction/article.md b/9-regular-expressions/01-regexp-introduction/article.md index 57fe02f5d..86c4bc703 100644 --- a/9-regular-expressions/01-regexp-introduction/article.md +++ b/9-regular-expressions/01-regexp-introduction/article.md @@ -1,119 +1,179 @@ -# Patterns and flags +# Kalıplar ve işaretler -A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*. +Düzenli ifadeler, metinde arama ve değiştirme yapmak için etkili bir yöntem sağlayan kalıplardır. -There are two syntaxes to create a regular expression object. +JavaScript'te, [RegExp](mdn:js/RegExp) nesnesi aracılığıyla kullanılabilirler ve ayrıca string metotlarına entegre edilmişlerdir. -The long syntax: +## Düzenli İfadeler + +Bir düzenli ifade ("regexp" veya sadece "reg") bir _kalıp_ ve isteğe bağlı *işaretler*den oluşur. + +Düzenli ifade nesnesi oluşturmak için kullanılabilecek iki sözdizimi vardır. + +Uzun olan sözdizimi: ```js -regexp = new RegExp("pattern", "flags"); +regexp = new RegExp("kalıp", "işaretler"); ``` -...And the short one, using slashes `"/"`: +Ve bölme işareti "/" kullanan kısa olanı: ```js -regexp = /pattern/; // no flags -regexp = /pattern/gmi; // with flags g,m and i (to be covered soon) +regexp = /kalıp/; // işaret yok +regexp = /kalıp/gim; // g, m ve i işaretleriyle birlikte (yakında ele alınacak) ``` -Slashes `"/"` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings. +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. -## Usage +Her iki durumda da `regexp`, `RegExp` sınıfının bir örneği oluşturulmuş olur. -To search inside a string, we can use method [search](mdn:js/String/search). +Bu ikisi arasındaki temel fark, bölme işareti `/.../` kullanan modelin kalıba expression eklenmesine izin vermemesidir (`${...}` kullanan string şablonları gibi). Tamamen statiktirler. -Here's an example: +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: -```js run -let str = "I love JavaScript!"; // will search here +```js +let tag = prompt("Hangi tag'i bulmak istiyorsun?", "h2"); -let regexp = /love/; -alert( str.search(regexp) ); // 2 +let regexp = new RegExp(`<${tag}>`); // yukarıdaki prompt'a "h2" cevabı verildiyse, /

/ ile aynı olur. ``` -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. +## İşaretler -The code above is the same as: +Düzenli ifadelerde aramayı etkileyen işaretler olabilir. -```js run -let str = "I love JavaScript!"; // will search here +JavaScript'te bunlardan sadece 6 tanesi var: -let substr = 'love'; -alert( str.search(substr) ); // 2 -``` +`pattern:i` +: 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). -So searching for `pattern:/love/` is the same as searching for `"love"`. +`pattern:g` +: 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. -But that's only for now. Soon we'll create more complex regular expressions with much more searching power. +`pattern:m` +: Çok satırlı mod ( bölümünde ele alınmıştır). -```smart header="Colors" -From here on the color scheme is: +`pattern:s` +: Yeni satır karakteri `\n` ile eşleşmek için noktaya `pattern:.` izin veren “dotall” modunu etkinleştirir. ( bölümünde ele alınmıştır). + +`pattern:u` +: Tam unicode desteğini etkinleştirir. Bu işaret, vekil çiftlerin doğru işlenmesini sağlar. Bununla ilgili daha fazla bilgi bölümünde. + +`pattern:y` +: “Yapışkan” mod: metindeki tam pozisyonu arar ( bölümünde ele alınmıştır) + +```smart header="Renkler" +Buradan itibaren renk şeması şu şekilde olacak: - regexp -- `pattern:red` -- string (where we search) -- `subject:blue` -- result -- `match:green` +- string (aradığımız yer) -- `subject:blue` +- sonuç -- `match:green` ``` +## Arama: str.match -````smart header="When to use `new RegExp`?" -Normally we use the short syntax `/.../`. But it does not support variable insertions `${...}`. +Daha önce de belirtildiği gibi, düzenli ifadeler string metotlarına entegre edilmiştir. -On the other hand, `new RegExp` allows to construct a pattern dynamically from a string, so it's more flexible. +`str.match(regexp)` metodu `regexp` ile `str` stringindeki tüm eşleşmeleri bulur. -Here's an example of a dynamically generated regexp: +3 çalışma modu vardır: -```js run -let tag = prompt("Which tag you want to search?", "h2"); -let regexp = new RegExp(`<${tag}>`); +1. Düzenli ifadede `pattern:g` işareti varsa, tüm eşleşmelerden oluşan bir dizi döndürür: -// finds

by default -alert( "

".search(regexp)); -``` -```` + ```js run + let str = "We will, we will rock you"; + + alert( str.match(/we/gi) ); // We,we (eşleşen 2 string'den oluşan bir dizi) + ``` + 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. + +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: + ```js run + let str = "We will, we will rock you"; + let result = str.match(/we/i); // g işareti olmadan -## Flags + alert( result[0] ); // We (ilk eşleşme) + alert( result.length ); // 1 -Regular expressions may have flags that affect the search. + // Details: + alert( result.index ); // 0 (eşlemenin string'deki pozisyonu) + alert( result.input ); // We will, we will rock you (kaynak string) + ``` + Düzenli ifadenin bir kısmı parantez içine alınmışsa dizinin `0` dışında başka indeksleri de olabilir. Bunu, bölümünde ele alacağız. -There are only 6 of them in JavaScript: +3. Son olarak, eşleşme yoksa `null` döndürülür (`pattern:g` işareti olup olmadığı önemli değildir). -`i` -: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below). + 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: -`g` -: With this flag the search looks for all matches, without it -- only the first one (we'll see uses in the next chapter). + ```js run + let matches = "JavaScript".match(/HTML/); // = null -`m` -: Multiline mode (covered in the chapter ). + if (!matches.length) { // Error: Cannot read property 'length' of null + alert("Yukardaki satırda hata var"); + } + ``` -`s` -: "Dotall" mode, allows `.` to match newlines (covered in the chapter ). + Sonucun her zaman bir dizi olmasını istiyorsak, bunu şu şekilde yazabiliriz: -`u` -: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter . + ```js run + let matches = "JavaScript".match(/HTML/)*!* || []*/!*; -`y` -: Sticky mode (covered in the chapter ) + if (!matches.length) { + alert("Eşleşme yok"); // şimdi çalışıyor + } + ``` -We'll cover all these flags further in the tutorial. +## Yer değiştirme: str.replace -For now, the simplest flag is `i`, here's an example: +`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ı). + +Örneğin: ```js run -let str = "I love JavaScript!"; +// no flag g +alert( "We will, we will".replace(/we/i, "I") ); // I will, we will + +// with flag g +alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will +``` + +İ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: -alert( str.search(/LOVE/i) ); // 2 (found lowercased) +| Semboller | Değiştirilen yeni_str'de yapılan eylem | +|--------|--------| +|`$&`|tüm eşleşmeyi ekler| +|$`|string'in eşleşmeden önce gelen kısmını ekler| +|`$'`|string'in eşleşmeden sonra gelen kısmını ekler| +|`$n`|`n` 1-2 basamaklı bir sayıysa, n. parantezlerin içeriğini ekler, bununla ilgili daha fazla bilgi bölümünde| +|`$`|`isim` adı verilen parantezlerin içeriğini ekler, daha fazlası bölümünde| +|`$$`|`$` karakterini ekler| + +`pattern:$&` ile bir örnek:: + +```js run +alert( "I love HTML".replace(/HTML/, "$& and JavaScript") ); // I love HTML and JavaScript +``` + +## Test: regexp.test + +`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. + + +```js run +let str = "I love JavaScript"; +let regexp = /LOVE/i; -alert( str.search(/LOVE/) ); // -1 (nothing found without 'i' flag) +alert( regexp.test(str) ); // true ``` -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. +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. +Metotlar hakkında tam bilgi makalesinde verilmiştir. -## Summary +## Özet -- A regular expression consists of a pattern and optional flags: `g`, `i`, `m`, `u`, `s`, `y`. -- Without flags and special symbols that we'll study later, the search by a regexp is the same as a substring search. -- 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. +- 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`. +- İşaretler ve özel semboller olmadan (daha sonra inceleyeceğiz) düzenli ifadelerle arama bir substring aramasıyla aynıdır. +- `str.match(regexp)` metodu eşleşmeleri arar: `pattern:g` işareti varsa tümü, aksi takdirde yalnızca birincisini döner. +- `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. +- `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. diff --git a/9-regular-expressions/index.md b/9-regular-expressions/index.md index 7499c584d..c0515aa19 100644 --- a/9-regular-expressions/index.md +++ b/9-regular-expressions/index.md @@ -1,7 +1,3 @@ -# Regular expressions +# Düzenli İfadeler -Regular expressions is a powerful way of doing search and replace in strings. - -In JavaScript regular expressions are implemented using objects of a built-in `RegExp` class and integrated with strings. - -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. +Düzenli ifadeler, dizelerde arama ve değiştirme yapmanın etkili bir yoludur.