JS RegExp क्वांटिफायर्स
In this page:
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 गिनी जाती है।
उदाहरण: Zero or More: *
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 से मैच कर लेता।
उदाहरण: One or More: +
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 को संभालते हुए।
उदाहरण: Zero or One: ?
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 करने जैसी चीज़ों के लिए उपयोगी है।
उदाहरण: Specific Ranges with {n,m}
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" कर सकता है।
उदाहरण: Greedy vs Lazy Matching
console.log("<a><b>".match(/<.*>/)[0]); // greedy: "<a><b>"
console.log("<a><b>".match(/<.*?>/)[0]); // lazy: "<a>"
- * (zero or more) इस्तेमाल करना जब असल में + (one or more) intended था, जिससे pattern किसी empty string को match कर लेता है जहाँ कम से कम एक character चाहिए था।
- जहाँ एक lazy quantifier चाहिए था वहाँ एक greedy quantifier लिखना, जिससे pattern intended से कहीं ज़्यादा text match कर लेता है -- जैसे nearest pair के बजाय पहले और आखिरी quotation mark के बीच सब कुछ match करना।
- यह भूल जाना कि 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 से इसका हिस्सा रहे हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML