Skip to content

Commit 33ded7d

Browse files
author
Md. Jamal Uddin
authored
Merge pull request #59 from msisaifu/patterns_and_flags
Patterns and flags
2 parents 42b8b41 + 316b9ca commit 33ded7d

File tree

1 file changed

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

1 file changed

+69
-69
lines changed
+69-69
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,108 @@
1-
# Patterns and flags
1+
# প্যাটার্ন এবং ফ্ল্যাগ
22

3-
Regular expressions are patterns that provide a powerful way to search and replace in text.
3+
টেক্সটে কোনকিছু খোঁজা অথবা রিপ্লেস করার একটি পাওয়ারফুল মাধ্যম হল রেগুলার এক্সপ্রেশন।
44

5-
In JavaScript, they are available via the [RegExp](mdn:js/RegExp) object, as well as being integrated in methods of strings.
5+
জাভাস্ক্রিপ্টে এটি [RegExp](mdn:js/RegExp) অবজেক্টের অধীনে আছে এবং স্ট্রিং এর মেথডগুলোর মধ্যে এদের ব্যবহার করতে পারি।
66

7-
## Regular Expressions
7+
## রেগুলার এক্সপ্রেশনস
88

9-
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
9+
রেগুলার এক্সপ্রেশন (যা "regexp" অথবা "reg" নামেও পরিচিত) হল *প্যাটার্ন* এবং অপশনাল *ফ্ল্যাগ* এর মাধ্যমে গঠিত একটি সমন্বিতরূপ।
1010

11-
There are two syntaxes that can be used to create a regular expression object.
11+
দুইভাবে আমরা রেগুলার এক্সপ্রেশন অবজেক্ট তৈরি করতে পারি।
1212

13-
The "long" syntax:
13+
"লং" সিনট্যাক্সঃ
1414

1515
```js
16-
regexp = new RegExp("pattern", "flags");
16+
regexp = new RegExp("প্যাটার্ন", "ফ্ল্যাগ");
1717
```
1818

19-
And the "short" one, using slashes `"/"`:
19+
এবং "শর্ট" সিনট্যাক্স যা `"/"` দ্বারা ডিক্লেয়ার করা হয়ঃ
2020

2121
```js
22-
regexp = /pattern/; // no flags
23-
regexp = /pattern/gmi; // with flags g,m and i (to be covered soon)
22+
regexp = /pattern/; // ফ্ল্যাগ ছাড়া
23+
regexp = /pattern/gmi; // ফ্ল্যাগ সহ g,m and i (এগুলো সম্পর্কে সামনেই জানতে পারব)
2424
```
2525

26-
Slashes `pattern:/.../` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
26+
স্ল্যাস `pattern:/.../` ব্যবহারের মাধ্যমে জাভাস্ক্রিপ্ট ইঞ্জিন বুঝতে পারে এটি একটি রেগুলার এক্সপ্রেশন। যেভাবে `""` অথবা `''` দ্বারা স্ট্রিং কে চিনতে পারে।
2727

28-
In both cases `regexp` becomes an instance of the built-in `RegExp` class.
28+
উভয়ক্ষেত্রে `regexp` বিল্টইন `RegExp` ক্লাস এর ইনস্ট্যান্স হিসেবে তৈরি হয়।
2929

30-
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.
30+
এই দুই সিনট্যাক্সের মূল পার্থক্য হল `/.../` এখানে ডাইনামিক্যালি কোন ভেরিয়েবল (যেমনঃ টেমপ্লেট লিটারেলস স্ট্রিং `${...}`) ব্যবহার করতে পারবেন না। এটি পুরোপুরি স্ট্যাটিক।
3131

32-
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:
32+
বেশিরভাগ ক্ষেত্রে কোড লিখার সময় যখন আমরা রেগুলার এক্সপ্রেশনটি জানি তখন স্ল্যাস ব্যবহার করি। আর `new RegExp` ব্যবহার করি যখন আমাদের প্যাটার্ন জেনারেট করতে হয় ডাইনামিক্যালি। উদাহরণস্বরুপঃ
3333

3434
```js
35-
let tag = prompt("What tag do you want to find?", "h2");
35+
let tag = prompt("আপনি কোন ট্যাগটি খুঁজতে চাচ্ছেন?", "h2");
3636

37-
let regexp = new RegExp(`<${tag}>`); // same as /<h2>/ if answered "h2" in the prompt above
37+
let regexp = new RegExp(`<${tag}>`); // এটি /<h2>/ এর মত যদি prompt এ ইনপুট h2 দেয়
3838
```
3939

40-
## Flags
40+
## ফ্ল্যাগ
4141

42-
Regular expressions may have flags that affect the search.
42+
রেগুলার এক্সপ্রেশনে কিছু ফ্ল্যাগ আছে যা সার্চিং এর সময় ব্যবহৃত হয়।
4343

44-
There are only 6 of them in JavaScript:
44+
জাভাস্ক্রিপ্টে ৬ ধরনের ফ্ল্যাগ আছেঃ
4545

4646
`pattern:i`
47-
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
47+
: এই ফ্ল্যাগটি কেস ইনসেনসিটিভ বুঝায়ঃ `A` এবং `a` এর মধ্যে কোন পার্থক্য নেই (নিচের উদাহরণ দেখুন)।
4848

4949
`pattern:g`
50-
: With this flag the search looks for all matches, without it -- only the first match is returned.
50+
: এই ফ্ল্যাগটি টেক্সটে সব মিল খুঁজে, এটি ছাড়া -- শুধু প্রথম মিলটি রিটার্ন করে।
5151

5252
`pattern:m`
53-
: Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
53+
: মাল্টিলাইন মোড (বিস্তারিত এই অধ্যায়ে <info:regexp-multiline-mode>)
5454

5555
`pattern:s`
56-
: Enables "dotall" mode, that allows a dot `pattern:.` to match newline character `\n` (covered in the chapter <info:regexp-character-classes>).
56+
: "dotall" মোড, নিউলাইন ক্যারেক্টার `\n` কে ডট `pattern:.` দিয়ে খোঁজা যায় (বিস্তারিত এই অধ্যায়ে <info:regexp-character-classes>).
5757

5858
`pattern:u`
59-
: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
59+
: ফুল ইউনিকোর্ড সাপোর্ট এনাবল করে। ফ্ল্যাগটি সঠিকভাবে সারোগেটজোড় কে প্রসেসিংয়ের সাপোর্ট দেয়। আরো জানতে পারবেন এই অধ্যায়ে <info:regexp-unicode>
6060

6161
`pattern:y`
62-
: "Sticky" mode: searching at the exact position in the text (covered in the chapter <info:regexp-sticky>)
62+
: "Sticky" মোডঃ সার্চিং এর সময় টেক্সটে এক্স্যাক্ট পজিশনটি জানা যায় (বিস্তারিত এই অধ্যায়ে <info:regexp-sticky>)
6363

64-
```smart header="Colors"
65-
From here on the color scheme is:
64+
```smart header="কালারস"
65+
এখানে কালার স্কিমটা হলঃ
6666
6767
- regexp -- `pattern:red`
68-
- string (where we search) -- `subject:blue`
68+
- string (যেখানে আমরা সার্চ করি) -- `subject:blue`
6969
- result -- `match:green`
7070
```
7171

72-
## Searching: str.match
72+
## সার্চিংঃ str.match
7373

74-
As mentioned previously, regular expressions are integrated with string methods.
74+
আমরা পূর্বেই জেনেছি, রেগুলার এক্সপ্রেশনগুলো স্ট্রিংয়ের মেথডগুলোর সাথে ইন্টিগ্রেটেড।
7575

76-
The method `str.match(regexp)` finds all matches of `regexp` in the string `str`.
76+
`str.match(regexp)` মেথডটির সাহায্যে `str` এর মধ্যে `regexp` এর সকল মিল খুঁজা যায়।
7777

78-
It has 3 working modes:
78+
এটির ৩ ধরণের মোড আছেঃ
7979

80-
1. If the regular expression has flag `pattern:g`, it returns an array of all matches:
80+
1. যদি রেগুলার এক্সপ্রেশনে `pattern:g` এই ফ্ল্যাগটি ব্যবহৃত হয়, এটি সব মিলপ্রাপ্ত প্যাটার্নগুলোকে অ্যারেতে রিটার্ন করেঃ
8181
```js run
8282
let str = "We will, we will rock you";
8383

84-
alert( str.match(/we/gi) ); // We,we (an array of 2 substrings that match)
84+
alert( str.match(/we/gi) ); // We,we (অ্যারে আকারে এই ২টি সাবস্ট্রিং রিটার্ন করবে)
8585
```
86-
Please note that both `match:We` and `match:we` are found, because flag `pattern:i` makes the regular expression case-insensitive.
86+
লক্ষ্য করুন এখানে `match:We` এবং `match:we` দুটিই দেখাবে, যেহেতু আমরা কেস-ইনসেনসিটিভ ফ্ল্যাগটি ব্যবহার করেছি।
8787

88-
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:
88+
2. যদি আমরা কোন ফ্ল্যাগ ব্যবহার না করি তাহলে এটি প্রথম মিলটিকে অ্যারে আকারে রিটার্ন করবে, সম্পূর্ন মিলটিকে আমরা ইনডেক্স `0` তে পাই এবং প্রপার্টিগুলোর অতিরিক্ত কিছু বর্ননাঃ
8989
```js run
9090
let str = "We will, we will rock you";
9191
92-
let result = str.match(/we/i); // without flag g
92+
let result = str.match(/we/i); // ফ্ল্যাগ g ছাড়া
9393
94-
alert( result[0] ); // We (1st match)
94+
alert( result[0] ); // We (১ম মিল)
9595
alert( result.length ); // 1
9696
9797
// Details:
98-
alert( result.index ); // 0 (position of the match)
98+
alert( result.index ); // 0 (প্রাপ্ত মিলের পজিশন)
9999
alert( result.input ); // We will, we will rock you (source string)
100100
```
101-
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 <info:regexp-groups>.
101+
`0` এর পাশাপাশি অ্যারের অন্যান্য ইনডেক্সও থাকতে পারে যদি এক্সপ্রেশনটিতে প্রথম বন্ধনী থাকে। আমরা এই অধ্যায়ে এসম্পর্কে বিস্তারিত জানতে পারব <info:regexp-groups>
102102

103-
3. And, finally, if there are no matches, `null` is returned (doesn't matter if there's flag `pattern:g` or not).
103+
3. এবং, সর্বশেষে যদি কোন মিল খুঁজে পাওয়া না যায় `null` রিটার্ন করবে (এটি `pattern:g` ফ্ল্যাগের উপর নির্ভর করে না)।
104104

105-
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.:
105+
একটি গুরুত্বপূর্ন ব্যাপার খেয়াল রাখা উচিত। কোন মিল খুঁজে পাওয়া না গেলে এম্পটি স্ট্রিং এর বদলে `null` রিটার্ন করে। এজন্য এম্পটি চেকিংয়ের জন্য ভুলভাবে চেকিংয়ের জন্য এরর পেতে পারেন, যেমনঃ
106106

107107
```js run
108108
let matches = "JavaScript".match(/HTML/); // = null
@@ -112,50 +112,50 @@ It has 3 working modes:
112112
}
113113
```
114114

115-
If we'd like the result to always be an array, we can write it this way:
115+
নিম্নোক্ত উপায়ে রেজাল্ট আমরা সর্বদা অ্যারে হিসেবে চেক করতে পারিঃ
116116

117117
```js run
118118
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
119119
120120
if (!matches.length) {
121-
alert("No matches"); // now it works
121+
alert("No matches"); // এখন এটি কাজ করবে
122122
}
123123
```
124124

125-
## Replacing: str.replace
125+
## রিপ্লেসিংঃ str.replace
126126

127-
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).
127+
`str.replace(regexp, replacement)` মেথডটি `regexp` দ্বারা প্রাপ্ত সব মিলকে `replacement` করে `pattern:g` ফ্ল্যাগের জন্য, অন্যথায় শুধু প্রথম মিলটিকে রিপ্লেস করে।
128128

129-
For instance:
129+
উদাহরণস্বরূপঃ
130130

131131
```js run
132-
// no flag g
132+
// g ফ্ল্যাগ ছাড়া
133133
alert( "We will, we will".replace(/we/i, "I") ); // I will, we will
134134
135-
// with flag g
135+
// g ফ্ল্যাগ সহ
136136
alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will
137137
```
138138

139-
The second argument is the `replacement` string. We can use special character combinations in it to insert fragments of the match:
139+
দ্বিতীয় আর্গুমেন্টে `replacement` স্ট্রিংটি পাস করা হয়। মিল খুঁজে পাওয়া অংশগুলো `replacement` এর সাথে ব্যবহার করতে আমরা কিছু স্পেশাল ক্যারেক্টার সংযুক্ত করতে পারি।
140140

141-
| Symbols | Action in the replacement string |
141+
| সিম্বলস | রিপ্লসমেন্ট স্ট্রিংয়ে সংগঠিত অ্যাকশন |
142142
|--------|--------|
143-
|`$&`|inserts the whole match|
144-
|<code>$&#096;</code>|inserts a part of the string before the match|
145-
|`$'`|inserts a part of the string after the match|
146-
|`$n`|if `n` is a 1-2 digit number, then it inserts the contents of n-th parentheses, more about it in the chapter <info:regexp-groups>|
147-
|`$<name>`|inserts the contents of the parentheses with the given `name`, more about it in the chapter <info:regexp-groups>|
148-
|`$$`|inserts character `$` |
143+
|`$&`|পুরো মিলটিকে সংযুক্ত করে|
144+
|<code>$&#096;</code>|মিলের আগ পর্যন্ত স্ট্রিংকেও সংযুক্ত করে|
145+
|`$'`|মিলের পরের স্ট্রিংকেও সংযুক্ত করে|
146+
|`$n`|যদি `n` -২ ডিজিটের নাম্বার হয়, আমরা বন্ধনী দ্বারা পাওয়া মানগুলোকে `$n` সংযুক্ত করতে পারি, বিস্তারিত এই অধ্যায়ে <info:regexp-groups>|
147+
|`$<name>`|আমরা বন্ধনী দ্বারা পাওয়া মানগুলোকে `name` সংযুক্ত করতে পারি,বিস্তারিত এই অধ্যায়ে <info:regexp-groups>|
148+
|`$$`|`$` ক্যারাক্টারটি সংযুক্ত করতে|
149149

150-
An example with `pattern:$&`:
150+
`pattern:$&` এর একটি উদাহরণঃ
151151

152152
```js run
153153
alert( "I love HTML".replace(/HTML/, "$& and JavaScript") ); // I love HTML and JavaScript
154154
```
155155

156-
## Testing: regexp.test
156+
## টেস্টিংঃ regexp.test
157157

158-
The method `regexp.test(str)` looks for at least one match, if found, returns `true`, otherwise `false`.
158+
`regexp.test(str)` মেথডটি অন্তত একটি মিল খুঁজে পেলে `true` রিটার্ন করে, অন্যথায় `false` রিটার্ন করে।
159159

160160
```js run
161161
let str = "I love JavaScript";
@@ -164,14 +164,14 @@ let regexp = /LOVE/i;
164164
alert( regexp.test(str) ); // true
165165
```
166166

167-
Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods.
167+
পরবর্তী অধ্যায়গুলোতে রেগুলার এক্সপ্রেশন সম্পর্কে আমরা আরো অনেক কিছু শিখব, আরো অনেক উদাহরণ এবং রেগুলার এক্সপ্রেশনের অন্যান্য মেথডগুলোও জানব।
168168

169-
Full information about the methods is given in the article <info:regexp-methods>.
169+
মেথডগুলো সম্পর্কে বিস্তারিত আমরা এই অধ্যায়ে জানব <info:regexp-methods>.
170170

171-
## Summary
171+
## সারাংশ
172172

173-
- A regular expression consists of a pattern and optional flags: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
174-
- Without flags and special symbols (that we'll study later), the search by a regexp is the same as a substring search.
175-
- The method `str.match(regexp)` looks for matches: all of them if there's `pattern:g` flag, otherwise, only the first one.
176-
- 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.
177-
- The method `regexp.test(str)` returns `true` if there's at least one match, otherwise, it returns `false`.
173+
- রেগুলার এক্সপ্রেশন গঠিত হয় প্যাটার্ন এবং অপশনাল ফ্ল্যাগের সমন্বয়েঃ `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`
174+
- ফ্ল্যাগ এবং স্পেশাল সিম্বল ছাড়া (পরবর্তীতে জানব) regexp এর সার্চ substring সার্চ এর মত কাজ করে.
175+
- `str.match(regexp)` মেথডটি `pattern:g` ফ্ল্যাগের জন্য প্যাটার্নের সাথে সকল মিল খুঁজে, অন্যথায় শুধু প্রথম মিলটি খুঁজে।
176+
- `str.replace(regexp, replacement)` মেথডটি `regexp` দ্বারা প্রাপ্ত সব মিলকে `replacement` করে `pattern:g` ফ্ল্যাগের জন্য, অন্যথায় শুধু প্রথম মিলটিকে রিপ্লেস করে।
177+
- `regexp.test(str)` মেথডটি অন্তত একটি মিল খুঁজে পেলে `true` রিটার্ন করে, অন্যথায় `false` রিটার্ন করে।

0 commit comments

Comments
 (0)