From 527cc6927688414984f275b5b7ce7ef9fb93ff52 Mon Sep 17 00:00:00 2001 From: Saiful Date: Thu, 14 May 2020 00:42:14 +0600 Subject: [PATCH 1/2] wip patterns and flags --- .../01-regexp-introduction/article.md | 121 +++++++++--------- 1 file changed, 60 insertions(+), 61 deletions(-) diff --git a/9-regular-expressions/01-regexp-introduction/article.md b/9-regular-expressions/01-regexp-introduction/article.md index a35d19a7b..dd2489a3d 100644 --- a/9-regular-expressions/01-regexp-introduction/article.md +++ b/9-regular-expressions/01-regexp-introduction/article.md @@ -1,67 +1,67 @@ -# Patterns and flags +# প্যাটার্ন এবং ফ্ল্যাগ -Regular expressions are patterns that provide a powerful way to search and replace in text. +টেক্সটে কোনকিছু খোঁজা অথবা রিপ্লেস করার একটি পাওয়ারফুল মাধ্যম হল রেগুলার এক্সপ্রেশন। -In JavaScript, they are available via the [RegExp](mdn:js/RegExp) object, as well as being integrated in methods of strings. +জাভাস্ক্রীপ্টে এটি [RegExp](mdn:js/RegExp) অবজেক্টের অধীনে আছে এবং স্ট্রিং এর মেথডগুলোর মধ্যে এদের ব্যবহার করতে পারি। -## Regular Expressions +## রেগুলার এক্সপ্রেশনস -A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*. +রেগুলার এক্সপ্রেশন (যা "regexp" অথবা "reg" নামেও পরিচিত) হল *প্যাটার্ন* এবং অপশনাল *ফ্ল্যাগ* এর মাধ্যমে গঠিত একটি সমন্বিতরূপ। -There are two syntaxes that can be used to create a regular expression object. +দুইভাবে আমরা রেগুলার এক্সপ্রেশন অবজেক্ট তৈরি করতে পারি। -The "long" syntax: +"লং" সিনট্যাক্সঃ ```js -regexp = new RegExp("pattern", "flags"); +regexp = new RegExp("প্যাটার্ন", "ফ্ল্যাগ"); ``` -And the "short" one, using slashes `"/"`: +এবং "শর্ট" সিনট্যাক্স যা `"/"` দ্বারা ডিক্লেয়ার করা হয়ঃ ```js -regexp = /pattern/; // no flags -regexp = /pattern/gmi; // with flags g,m and i (to be covered soon) +regexp = /pattern/; // ফ্ল্যাগ ছাড়া +regexp = /pattern/gmi; // ফ্ল্যাগ সহ g,m and i (এগুলো সম্পর্কে সামনেই জানতে পারব) ``` -Slashes `pattern:/.../` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings. +স্ল্যাস `pattern:/.../` ব্যবহারের মাধ্যমে জাভাস্ক্রীপ্ট ইঞ্জিন বুঝতে পারে এটি একটি রেগুলার এক্সপ্রেশন। যেভাবে `""` অথবা `''` দ্বারা স্ট্রীং কে চিনতে পারে। -In both cases `regexp` becomes an instance of the built-in `RegExp` class. +উভয়ক্ষেত্রে `regexp` বিল্টইন `RegExp` ক্লাস এর ইনস্ট্যান্স হিসেবে তৈরি হয়। -The main difference between these two syntaxes is that pattern using slashes `/.../` does not allow for expressions to be inserted (like string template literals with `${...}`). They are fully static. +এই দুই সিনট্যাক্সের মূল পার্থক্য হল `/.../` এখানে ডাইনামিক্যালি কোন ভেরিয়েবল (যেমনঃ টেমপ্লেট লিটারেলস স্ট্রিং `${...}`) ব্যবহার করতে পারবেন না। এটি পুরোপুরি স্ট্যাটিক। Slashes are used when we know the regular expression at the code writing time -- and that's the most common situation. While `new RegExp`, is more often used when we need to create a regexp "on the fly" from a dynamically generated string. For instance: ```js -let tag = prompt("What tag do you want to find?", "h2"); +let tag = prompt("আপনি কোন ট্যাগটি খুঁজতে চাচ্ছেন?", "h2"); -let regexp = new RegExp(`<${tag}>`); // same as /

/ if answered "h2" in the prompt above +let regexp = new RegExp(`<${tag}>`); // এটি /

/ এর মত যদি prompt এ ইনপুট h2 দেয় ``` -## Flags +## ফ্ল্যাগ -Regular expressions may have flags that affect the search. +রেগুলার এক্সপ্রেশনে কিছু ফ্ল্যাগ আছে যা সার্চিং এর সময় ব্যবহৃত হয়। -There are only 6 of them in JavaScript: +জাভাস্ক্রিপ্টে ৬ ধরনের ফ্ল্যাগ আছেঃ `pattern:i` -: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below). +: এই ফ্ল্যাগটি কেস ইনসেনসিটিভ বুঝায়ঃ `A` এবং `a` এর মধ্যে কোন পার্থক্য নেই (নিচের উদাহরণ দেখুন)। `pattern:g` -: With this flag the search looks for all matches, without it -- only the first match is returned. +: এই ফ্ল্যাগটি টেক্সটে সব মিল খুঁজে, এটি ছাড়া -- শুধু প্রথম মিলটি রিটার্ন করে। `pattern:m` -: Multiline mode (covered in the chapter ). +: মাল্টিলাইন মোড (বিস্তারিত এই অধ্যায়ে )। `pattern:s` -: Enables "dotall" mode, that allows a dot `pattern:.` to match newline character `\n` (covered in the chapter ). +: "dotall" মোড, নিউলাইন ক্যারেক্টার `\n` কে ডট `pattern:.` দিয়ে খোঁজা যায় (বিস্তারিত এই অধ্যায়ে ). `pattern:u` -: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter . +: ফুল ইউনিকোর্ড সাপোর্ট এনাবল করে। ফ্ল্যাগটি সঠিকভাবে সারোগেটজোড় কে প্রসেসিংয়ের সাপোর্ট দেয়। আরো জানতে পারবেন এই অধ্যায়ে । `pattern:y` -: "Sticky" mode: searching at the exact position in the text (covered in the chapter ) +: "Sticky" মোডঃ searching at the exact position in the text (বিস্তারিত এই অধ্যায়ে ) -```smart header="Colors" +```smart header="কালারস" From here on the color scheme is: - regexp -- `pattern:red` @@ -69,40 +69,40 @@ From here on the color scheme is: - result -- `match:green` ``` -## Searching: str.match +## সার্চিংঃ str.match -As mentioned previously, regular expressions are integrated with string methods. +আমরা পূর্বেই জেনেছি, রেগুলার এক্সপ্রেশনগুলো স্ট্রিংয়ের মেথডগুলোর সাথে ইন্টিগ্রেটেড। -The method `str.match(regexp)` finds all matches of `regexp` in the string `str`. +`str.match(regexp)` মেথডটির সাহায্যে `str` এর মধ্যে `regexp` এর সকল মিল খুঁজা যায়। -It has 3 working modes: +এটির ৩ ধরণের মোড আছেঃ -1. If the regular expression has flag `pattern:g`, it returns an array of all matches: +1. যদি রেগুলার এক্সপ্রেশনে `pattern:g` এই ফ্ল্যাগটি ব্যবহৃত হয়, এটি সব মিলপ্রাপ্ত প্যাটার্নগুলোকে অ্যারেতে রিটার্ন করেঃ ```js run let str = "We will, we will rock you"; - alert( str.match(/we/gi) ); // We,we (an array of 2 substrings that match) + alert( str.match(/we/gi) ); // We,we (অ্যারে আকারে এই ২টি সাবস্ট্রিং রিটার্ন করবে) ``` - Please note that both `match:We` and `match:we` are found, because flag `pattern:i` makes the regular expression case-insensitive. + লক্ষ্য করুন এখানে `match:We` এবং `match:we` দুটিই দেখাবে, যেহেতু আমরা কেস-ইনসেনসিটিভ ফ্ল্যাগটি ব্যবহার করেছি। -2. If there's no such flag it returns only the first match in the form of an array, with the full match at index `0` and some additional details in properties: +2. যদি আমরা কোন ফ্ল্যাগ ব্যবহার না করি তাহলে এটি প্রথম মিলটিকে অ্যারে আকারে রিটার্ন করবে, সম্পূর্ন মিলটিকে আমরা ইনডেক্স `0` তে পাই এবং প্রপার্টিগুলোর অতিরিক্ত কিছু বর্ননাঃ ```js run let str = "We will, we will rock you"; - let result = str.match(/we/i); // without flag g + let result = str.match(/we/i); // ফ্ল্যাগ g ছাড়া - alert( result[0] ); // We (1st match) + alert( result[0] ); // We (১ম মিল) alert( result.length ); // 1 // Details: - alert( result.index ); // 0 (position of the match) + alert( result.index ); // 0 (প্রাপ্ত মিলের পজিশন) alert( result.input ); // We will, we will rock you (source string) ``` The array may have other indexes, besides `0` if a part of the regular expression is enclosed in parentheses. We'll cover that in the chapter . -3. And, finally, if there are no matches, `null` is returned (doesn't matter if there's flag `pattern:g` or not). +3. এবং, সর্বশেষে যদি কোন মিল খুঁজে পাওয়া না যায় `null` রিটার্ন করবে (এটি `pattern:g` ফ্ল্যাগের উপর নির্ভর করে না)। - This a very important nuance. If there are no matches, we don't receive an empty array, but instead receive `null`. Forgetting about that may lead to errors, e.g.: + একটি গুরুত্বপূর্ন ব্যাপার খেয়াল রাখা উচিত। যদি কোন মিল খুঁজে পাওয়া না গেলে এম্পটি স্ট্রিং এর বদলে `null` রিটার্ন করে। এজন্য এম্পটি চেকিংয়ের জন্য ভুলভাবে চেকিংয়ের জন্য এরর পেতে পারেন, যেমনঃ ```js run let matches = "JavaScript".match(/HTML/); // = null @@ -112,50 +112,48 @@ It has 3 working modes: } ``` - If we'd like the result to always be an array, we can write it this way: + নিম্নোক্ত উপায়ে রেজাল্ট আমরা সর্বদা অ্যারে হিসেবে চেক করতে পারিঃ ```js run let matches = "JavaScript".match(/HTML/)*!* || []*/!*; if (!matches.length) { - alert("No matches"); // now it works + alert("No matches"); // এখন এটি কাজ করবে } ``` -## Replacing: str.replace +## রিপ্লেসিংঃ str.replace -The method `str.replace(regexp, replacement)` replaces matches found using `regexp` in string `str` with `replacement` (all matches if there's flag `pattern:g`, otherwise, only the first one). +`str.replace(regexp, replacement)` মেথডটি `regexp` দ্বারা প্রাপ্ত সব মিলকে `replacement` করে `pattern:g` ফ্ল্যাগের জন্য, অন্যথায় শুধু প্রথম মিলটিকে রিপ্লেস করে। -For instance: +উদাহরণস্বরূপঃ ```js run -// no flag g +// g ফ্ল্যাগ ছাড়া alert( "We will, we will".replace(/we/i, "I") ); // I will, we will -// with flag g +// g ফ্ল্যাগ সহ alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will ``` - -The second argument is the `replacement` string. We can use special character combinations in it to insert fragments of the match: - -| Symbols | Action in the replacement string | +দ্বিতীয় আর্গুমেন্টে `replacement` স্ট্রিংটি পাস করা হয়। মিল খুঁজে পাওয়া অংশগুলো `replacement` এর সাথে ব্যবহার করতে আমরা কিছু স্পেশাল ক্যারেক্টার সংযুক্ত করতে পারি। +| সিম্বলস | রিপ্লসমেন্ট স্ট্রিংয়ে সংগঠিত অ্যাকশন | |--------|--------| -|`$&`|inserts the whole match| +|`$&`|পুরো মিলটিকে সংযুক্ত করে| |$`|inserts a part of the string before the match| |`$'`|inserts a part of the string after the match| |`$n`|if `n` is a 1-2 digit number, then it inserts the contents of n-th parentheses, more about it in the chapter | |`$`|inserts the contents of the parentheses with the given `name`, more about it in the chapter | |`$$`|inserts character `$` | -An example with `pattern:$&`: +`pattern:$&` এর একটি উদাহরণঃ ```js run alert( "I love HTML".replace(/HTML/, "$& and JavaScript") ); // I love HTML and JavaScript ``` -## Testing: regexp.test +## টেস্টিংঃ regexp.test -The method `regexp.test(str)` looks for at least one match, if found, returns `true`, otherwise `false`. +`regexp.test(str)` মেথডটি অন্তত একটি মিল খুঁজে পেলে `true` রিটার্ন করে, অন্যথায় `false` রিটার্ন করে। ```js run let str = "I love JavaScript"; @@ -164,14 +162,15 @@ let regexp = /LOVE/i; alert( regexp.test(str) ); // true ``` -Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods. +পরবর্তী অধ্যায়গুলোতে রেগুলার এক্সপ্রেশন সম্পর্কে আমরা আরো অনেক কিছু শিখব, আরো অনেক উদাহরণ এবং রেগুলার এক্সপ্রেশনের অন্যান্য মেথডগুলোও জানব। + +মেথডগুলো সম্পর্কে বিস্তারিত আমরা এই অধ্যায়ে জানব . -Full information about the methods is given in the article . -## Summary +## সারাংশ -- A regular expression consists of a pattern and optional flags: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`. +- রেগুলার এক্সপ্রেশন গঠিত হয় প্যাটার্ন এবং অপশনাল ফ্ল্যাগের সমন্বয়েঃ `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern: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.match(regexp)` looks for matches: all of them if there's `pattern:g` flag, otherwise, only the first one. -- The method `str.replace(regexp, replacement)` replaces matches found using `regexp` with `replacement`: all of them if there's `pattern:g` flag, otherwise only the first one. -- The method `regexp.test(str)` returns `true` if there's at least one match, otherwise, it returns `false`. +- `str.match(regexp)` মেথডটি `pattern:g` ফ্ল্যাগের জন্য প্যাটার্নের সাথে সকল মিল খুঁজে, অন্যথায় শুধু প্রথম মিলটি খুঁজে। +- `str.replace(regexp, replacement)` মেথডটি `regexp` দ্বারা প্রাপ্ত সব মিলকে `replacement` করে `pattern:g` ফ্ল্যাগের জন্য, অন্যথায় শুধু প্রথম মিলটিকে রিপ্লেস করে। +- `regexp.test(str)` মেথডটি অন্তত একটি মিল খুঁজে পেলে `true` রিটার্ন করে, অন্যথায় `false` রিটার্ন করে। From 316b9ca9eeba2a373322470e688358449277474b Mon Sep 17 00:00:00 2001 From: Saiful Date: Fri, 15 May 2020 01:54:05 +0600 Subject: [PATCH 2/2] done patterns and flags --- .../01-regexp-introduction/article.md | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/9-regular-expressions/01-regexp-introduction/article.md b/9-regular-expressions/01-regexp-introduction/article.md index dd2489a3d..4eed03e91 100644 --- a/9-regular-expressions/01-regexp-introduction/article.md +++ b/9-regular-expressions/01-regexp-introduction/article.md @@ -2,7 +2,7 @@ টেক্সটে কোনকিছু খোঁজা অথবা রিপ্লেস করার একটি পাওয়ারফুল মাধ্যম হল রেগুলার এক্সপ্রেশন। -জাভাস্ক্রীপ্টে এটি [RegExp](mdn:js/RegExp) অবজেক্টের অধীনে আছে এবং স্ট্রিং এর মেথডগুলোর মধ্যে এদের ব্যবহার করতে পারি। +জাভাস্ক্রিপ্টে এটি [RegExp](mdn:js/RegExp) অবজেক্টের অধীনে আছে এবং স্ট্রিং এর মেথডগুলোর মধ্যে এদের ব্যবহার করতে পারি। ## রেগুলার এক্সপ্রেশনস @@ -23,13 +23,13 @@ regexp = /pattern/; // ফ্ল্যাগ ছাড়া regexp = /pattern/gmi; // ফ্ল্যাগ সহ g,m and i (এগুলো সম্পর্কে সামনেই জানতে পারব) ``` -স্ল্যাস `pattern:/.../` ব্যবহারের মাধ্যমে জাভাস্ক্রীপ্ট ইঞ্জিন বুঝতে পারে এটি একটি রেগুলার এক্সপ্রেশন। যেভাবে `""` অথবা `''` দ্বারা স্ট্রীং কে চিনতে পারে। +স্ল্যাস `pattern:/.../` ব্যবহারের মাধ্যমে জাভাস্ক্রিপ্ট ইঞ্জিন বুঝতে পারে এটি একটি রেগুলার এক্সপ্রেশন। যেভাবে `""` অথবা `''` দ্বারা স্ট্রিং কে চিনতে পারে। উভয়ক্ষেত্রে `regexp` বিল্টইন `RegExp` ক্লাস এর ইনস্ট্যান্স হিসেবে তৈরি হয়। এই দুই সিনট্যাক্সের মূল পার্থক্য হল `/.../` এখানে ডাইনামিক্যালি কোন ভেরিয়েবল (যেমনঃ টেমপ্লেট লিটারেলস স্ট্রিং `${...}`) ব্যবহার করতে পারবেন না। এটি পুরোপুরি স্ট্যাটিক। -Slashes are used when we know the regular expression at the code writing time -- and that's the most common situation. While `new RegExp`, is more often used when we need to create a regexp "on the fly" from a dynamically generated string. For instance: +বেশিরভাগ ক্ষেত্রে কোড লিখার সময় যখন আমরা রেগুলার এক্সপ্রেশনটি জানি তখন স্ল্যাস ব্যবহার করি। আর `new RegExp` ব্যবহার করি যখন আমাদের প্যাটার্ন জেনারেট করতে হয় ডাইনামিক্যালি। উদাহরণস্বরুপঃ ```js let tag = prompt("আপনি কোন ট্যাগটি খুঁজতে চাচ্ছেন?", "h2"); @@ -59,13 +59,13 @@ let regexp = new RegExp(`<${tag}>`); // এটি /

/ এর মত যদি : ফুল ইউনিকোর্ড সাপোর্ট এনাবল করে। ফ্ল্যাগটি সঠিকভাবে সারোগেটজোড় কে প্রসেসিংয়ের সাপোর্ট দেয়। আরো জানতে পারবেন এই অধ্যায়ে । `pattern:y` -: "Sticky" মোডঃ searching at the exact position in the text (বিস্তারিত এই অধ্যায়ে ) +: "Sticky" মোডঃ সার্চিং এর সময় টেক্সটে এক্স্যাক্ট পজিশনটি জানা যায় (বিস্তারিত এই অধ্যায়ে ) ```smart header="কালারস" -From here on the color scheme is: +এখানে কালার স্কিমটা হলঃ - regexp -- `pattern:red` -- string (where we search) -- `subject:blue` +- string (যেখানে আমরা সার্চ করি) -- `subject:blue` - result -- `match:green` ``` @@ -98,11 +98,11 @@ From here on the color scheme is: alert( result.index ); // 0 (প্রাপ্ত মিলের পজিশন) alert( result.input ); // We will, we will rock you (source string) ``` - The array may have other indexes, besides `0` if a part of the regular expression is enclosed in parentheses. We'll cover that in the chapter . + `0` এর পাশাপাশি অ্যারের অন্যান্য ইনডেক্সও থাকতে পারে যদি এক্সপ্রেশনটিতে প্রথম বন্ধনী থাকে। আমরা এই অধ্যায়ে এসম্পর্কে বিস্তারিত জানতে পারব । 3. এবং, সর্বশেষে যদি কোন মিল খুঁজে পাওয়া না যায় `null` রিটার্ন করবে (এটি `pattern:g` ফ্ল্যাগের উপর নির্ভর করে না)। - একটি গুরুত্বপূর্ন ব্যাপার খেয়াল রাখা উচিত। যদি কোন মিল খুঁজে পাওয়া না গেলে এম্পটি স্ট্রিং এর বদলে `null` রিটার্ন করে। এজন্য এম্পটি চেকিংয়ের জন্য ভুলভাবে চেকিংয়ের জন্য এরর পেতে পারেন, যেমনঃ + একটি গুরুত্বপূর্ন ব্যাপার খেয়াল রাখা উচিত। কোন মিল খুঁজে পাওয়া না গেলে এম্পটি স্ট্রিং এর বদলে `null` রিটার্ন করে। এজন্য এম্পটি চেকিংয়ের জন্য ভুলভাবে চেকিংয়ের জন্য এরর পেতে পারেন, যেমনঃ ```js run let matches = "JavaScript".match(/HTML/); // = null @@ -135,15 +135,17 @@ alert( "We will, we will".replace(/we/i, "I") ); // I will, we will // g ফ্ল্যাগ সহ alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will ``` + দ্বিতীয় আর্গুমেন্টে `replacement` স্ট্রিংটি পাস করা হয়। মিল খুঁজে পাওয়া অংশগুলো `replacement` এর সাথে ব্যবহার করতে আমরা কিছু স্পেশাল ক্যারেক্টার সংযুক্ত করতে পারি। + | সিম্বলস | রিপ্লসমেন্ট স্ট্রিংয়ে সংগঠিত অ্যাকশন | |--------|--------| |`$&`|পুরো মিলটিকে সংযুক্ত করে| -|$`|inserts a part of the string before the match| -|`$'`|inserts a part of the string after the match| -|`$n`|if `n` is a 1-2 digit number, then it inserts the contents of n-th parentheses, more about it in the chapter | -|`$`|inserts the contents of the parentheses with the given `name`, more about it in the chapter | -|`$$`|inserts character `$` | +|$`|মিলের আগ পর্যন্ত স্ট্রিংকেও সংযুক্ত করে| +|`$'`|মিলের পরের স্ট্রিংকেও সংযুক্ত করে| +|`$n`|যদি `n` ১-২ ডিজিটের নাম্বার হয়, আমরা বন্ধনী দ্বারা পাওয়া মানগুলোকে `$n` সংযুক্ত করতে পারি, বিস্তারিত এই অধ্যায়ে | +|`$`|আমরা বন্ধনী দ্বারা পাওয়া মানগুলোকে `name` সংযুক্ত করতে পারি,বিস্তারিত এই অধ্যায়ে | +|`$$`|`$` ক্যারাক্টারটি সংযুক্ত করতে| `pattern:$&` এর একটি উদাহরণঃ @@ -166,11 +168,10 @@ alert( regexp.test(str) ); // true মেথডগুলো সম্পর্কে বিস্তারিত আমরা এই অধ্যায়ে জানব . - ## সারাংশ - রেগুলার এক্সপ্রেশন গঠিত হয় প্যাটার্ন এবং অপশনাল ফ্ল্যাগের সমন্বয়েঃ `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`। -- Without flags and special symbols (that we'll study later), the search by a regexp is the same as a substring search. +- ফ্ল্যাগ এবং স্পেশাল সিম্বল ছাড়া (পরবর্তীতে জানব) regexp এর সার্চ substring সার্চ এর মত কাজ করে. - `str.match(regexp)` মেথডটি `pattern:g` ফ্ল্যাগের জন্য প্যাটার্নের সাথে সকল মিল খুঁজে, অন্যথায় শুধু প্রথম মিলটি খুঁজে। - `str.replace(regexp, replacement)` মেথডটি `regexp` দ্বারা প্রাপ্ত সব মিলকে `replacement` করে `pattern:g` ফ্ল্যাগের জন্য, অন্যথায় শুধু প্রথম মিলটিকে রিপ্লেস করে। - `regexp.test(str)` মেথডটি অন্তত একটি মিল খুঁজে পেলে `true` রিটার্ন করে, অন্যথায় `false` রিটার্ন করে।