← Back to JavaScript Course | Chapter 9: Async & Web APIs | Lesson 24 of 26

JS RegExp क्वांटिफायर्स

Quantifiers यह नियंत्रित करते हैं कि किसी pattern का पहले वाला हिस्सा किसी match में गिने जाने के लिए कितनी बार दोहराना चाहिए -- zero या ज़्यादा के लिए *, एक या ज़्यादा के लिए +, zero या एक के लिए ?, और एक खास range के लिए {n,m} -- जो एक single-character pattern को उसी चीज़ के variable-length runs से मैच करने वाले pattern में बदल देते हैं।
Syntax
javascript
x*   // 0 or more
x+   // 1 or more
x?   // 0 or 1
x{n}   x{n,m}

Zero या ज़्यादा: *

* पहले वाले element की zero या ज़्यादा occurrences से मैच करता है -- ab*c "ac" (zero b) से, "abc" (एक b) से, "abbbc" (कई b) से मैच करता है, वगैरह, क्योंकि zero repetitions भी एक valid match गिनी जाती है।

Note: याद रखें कि * zero occurrences की इजाज़त देता है -- अगर असल में कम से कम एक चाहिए, तो इसके बजाय + इस्तेमाल करें।
Warning: एक pattern जो + का इरादा रखते हुए * पर निर्भर है, एक अनपेक्षित रूप से छोटी (या यहाँ तक कि खाली) string से मैच कर सकता है, क्योंकि zero repetitions भी * को संतुष्ट कर देती हैं।

उदाहरण: Zero or More: *

javascript
console.log(/ab*c/.test("ac"));    // true, zero b's
console.log(/ab*c/.test("abbbc")); // true, many b's

एक या ज़्यादा: +

+ पहले वाले element की कम से कम एक occurrence की मांग करता है -- \d+ "5", "42", या "123456" से मैच करता है, पर बिना digits वाली किसी string में कहीं भी बिल्कुल मैच नहीं करता, * के उलट जो technically अभी भी zero characters से मैच कर लेता।

Note: जब भी match के मायने रखने के लिए पहले वाले element का कम से कम एक बार आना सच में ज़रूरी हो, तो * के बजाय + इस्तेमाल करें।
Warning: + और * को मिला देना सबसे आम regex गलतियों में से एक है -- हमेशा दोबारा जांचें कि क्या आप जो मैच कर रहे हैं उसके लिए "zero" असल में एक valid count है।

उदाहरण: One or More: +

javascript
console.log(/\d+/.test("42"));  // true
console.log(/\d+/.test("abc")); // false, no digits at all

Zero या एक: ?

? पहले वाले element को optional बना देता है -- zero या एक बार मौजूद, कभी ज़्यादा नहीं -- colou?r "color" (बिना u के) और "colour" (उसके साथ) दोनों से मैच करता है, एक single pattern से American और British दोनों spellings को संभालते हुए।

Note: किसी genuinely optional single character या छोटे sequence को संभालने के लिए ? इस्तेमाल करें, जैसे कोई optional letter या कोई optional trailing character।
Warning: ? सिर्फ zero या एक occurrence की इजाज़त देता है -- इसका मतलब "optional और repeatable" नहीं है; उसके लिए, इसे एक group और एक अलग quantifier के साथ मिलाएं।

उदाहरण: Zero or One: ?

javascript
console.log(/colou?r/.test("color"));   // true
console.log(/colou?r/.test("colour"));  // true

{n,m} से खास Ranges

{n} ठीक n occurrences से मैच करता है, {n,} कम से कम n से, और {n,m} n और m occurrences (दोनों शामिल) के बीच से मैच करता है -- व्यापक */+/? shorthand से आगे repetition count पर सटीक control देते हुए, जो किसी fixed-length code को validate करने जैसी चीज़ों के लिए उपयोगी है।

Note: जब भी किसी repetition count की खास, जानी-पहचानी सीमाएं हों, जैसे 5-digit zip code या 8 से 20 characters की मांग करने वाला password, तो {n,m} इस्तेमाल करें।
Warning: {n,} या {n,m} का इरादा रखते हुए {n} लिखना (या उल्टा) एक आसान typo है जो चुपचाप matching rules बदल देता है -- हमेशा दोबारा जांचें कि आप असल में कौन सी bound(s) चाहते हैं।

उदाहरण: Specific Ranges with {n,m}

javascript
console.log(/^\d{3}$/.test("123"));   // true, exactly 3
console.log(/^\d{2,4}$/.test("12345")); // false, too many

Greedy बनाम Lazy Matching

डिफ़ॉल्ट रूप से, quantifiers greedy होते हैं -- वे overall pattern को सफल होने देते हुए ज़्यादा से ज़्यादा text मैच करते हैं।

किसी quantifier के ठीक बाद एक ? जोड़ना (जैसे *? या +?) इसे इसके बजाय lazy बना देता है, जितना कम हो सके text मैच करते हुए, जो तब मायने रखता है जब कोई pattern वरना इरादे से ज़्यादा text में "overshoot" कर सकता है।

Note: जब आप दो delimiters (जैसे quotes या HTML tags) के बीच के content को मैच कर रहे हों जो एक ही string में कई बार आ सकते हैं, तो पहले delimiter से लेकर आखिरी तक मैच होने से बचने के लिए एक lazy quantifier इस्तेमाल करें।
Warning: कई quoted sections वाली किसी string में "दो quotes के बीच" मैच करने के लिए इस्तेमाल किया गया एक greedy quantifier बिल्कुल पहले quote से बिल्कुल आखिरी तक मैच करेगा, सबसे पास वाले matching pair से नहीं।

उदाहरण: Greedy vs Lazy Matching

javascript
console.log("<a><b>".match(/<.*>/)[0]);  // greedy: "<a><b>"
console.log("<a><b>".match(/<.*?>/)[0]); // lazy: "<a>"
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. * (zero or more) इस्तेमाल करना जब असल में + (one or more) intended था, जिससे pattern किसी empty string को match कर लेता है जहाँ कम से कम एक character चाहिए था।
  2. जहाँ एक lazy quantifier चाहिए था वहाँ एक greedy quantifier लिखना, जिससे pattern intended से कहीं ज़्यादा text match कर लेता है -- जैसे nearest pair के बजाय पहले और आखिरी quotation mark के बीच सब कुछ match करना।
  3. यह भूल जाना कि comma के बिना {n,m} (सिर्फ {n}) का मतलब exactly n repetitions है, "up to n" नहीं।
चैप्टर सारांश
  • * zero या ज़्यादा match करता है, + एक या ज़्यादा match करता है, और ? preceding element में से zero या एक match करता है।
  • {n}, {n,}, और {n,m} क्रमशः exactly n, कम से कम n, या n और m के बीच repetitions match करते हैं।
  • डिफ़ॉल्ट रूप से quantifiers greedy होते हैं (जितना संभव हो उतना match करते हुए); उनके बाद ? जोड़ने से वे lazy बन जाते हैं (जितना कम संभव हो उतना match करते हुए)।
ब्राउज़र सपोर्ट

सभी standard regex quantifiers हर modern browser में supported हैं और JavaScript regex के सबसे शुरुआती implementation से इसका हिस्सा रहे हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.