Greedy and lazy quantifiers by msisaifu · Pull Request #199 · javascript-tutorial/bn.javascript.info

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greedy and lazy quantifiers #199

Changes from all commits

Commits

File filter

Filter by extension

Conversations

Failed to load comments.

Loading

Jump to

Jump to file

Failed to load files.

Loading

Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

The result is: `match:123 4`.
ফলাফলটি হল: `match:123 4`

First the lazy `pattern:\d+?` tries to take as little digits as it can, but it has to reach the space, so it takes `match:123`.
প্রথমে লেজি মোডে এই প্যাটার্নটি `pattern:\d+?` চেষ্টা করে যত কম সম্ভব অঙ্ক নেয়ার, কিন্তু প্যাটার্নের পরবর্তী স্পেস এর জন্য এটি স্পেস পর্যন্ত মেলে, সুতরাং এটি `match:123` এর সাথে মেলে।

Then the second `\d+?` takes only one digit, because that's enough.
অতঃপর পরবর্তী `\d+?` শুধু একটি অঙ্ক নেই, কেননা এরপর আর কোন প্যাটার্ন নেই।
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A match for /d+? d+?/
# /d+? d+?/ এর মিল

What's the match here?
এখানে অনুসন্ধানে কি পাওয়া যাবে?

```js
"123 456".match(/\d+? \d+?/g) ); // ?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
We need to find the beginning of the comment `match:<!--`, then everything till the end of `match:-->`.
আমাদের প্রথমে খুঁজা লাগবে কমেন্ট এর শুরুর `match:<!--` অংশ, এরপর কমেন্টের শেষ `match:-->` অংশ।

An acceptable variant is `pattern:<!--.*?-->` -- the lazy quantifier makes the dot stop right before `match:-->`. We also need to add flag `pattern:s` for the dot to include newlines.
সুতরাং প্যাটার্নটি হবে `pattern:<!--.*?-->` -- লেজি কোয়ান্টিফায়ারের ডটের জন্য এটি `match:-->` এর পূর্ব পর্যন্ত মেলে। আমাদের এই ফ্ল্যাগটি `pattern:s` দিতে হবে যেন ডট দ্বারা নিউলাইন ক্যারাক্টারকেও নির্দেশ করে।

Otherwise multiline comments won't be found:
অন্যথায় একের অধিক লাইনের কমেন্টগুলো অনুসন্ধানে আসবে না:

```js run
let regexp = /<!--.*?-->/gs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Find HTML comments
# এইচটিএমএল কমেন্ট অনুসন্ধান

Find all HTML comments in the text:
নিচের টেক্সট হতে সকল এইচটিএমএল কমেন্ট খুঁজার একটি প্যাটার্ন লিখুন:

```js
let regexp = /your regexp/g;
let regexp = /আপনার প্যাটার্ন/g;

let str = `... <!-- My -- comment
test --> .. <!----> ..
test --> .. <!----> ..
`;

alert( str.match(regexp) ); // '<!-- My -- comment \n test -->', '<!---->'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

The solution is `pattern:<[^<>]+>`.
সমাধানটি হল `pattern:<[^<>]+>`

```js run
let regexp = /<[^<>]+>/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Find HTML tags
# এইচটিএমএল ট্যাগের অনুসন্ধান

Create a regular expression to find all (opening and closing) HTML tags with their attributes.
একটি প্যাটার্ন লিখুন যেন সকল এইচটিএমএল ট্যাগগুলো(অ্যাট্রিবিউটসহ) খুঁজে পায়.

An example of use:
উদাহরণস্বরূপ:

```js run
let regexp = /your regexp/g;
let regexp = /আপনার প্যাটার্ন/g;

let str = '<> <a href="/"> <input type="radio" checked> <b>';

alert( str.match(regexp) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
```

Here we assume that tag attributes may not contain `<` and `>` (inside squotes too), that simplifies things a bit.
এখানে কিছুটা সহজের জন্য আমরা ধরে নিয়েছি অ্যাট্রিবিউটের মাঝে এই দুটি বন্ধনী `<` এবং `>` থাকবে না।