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

JS RegExp ग्रुप्स

किसी regex में parentheses सिर्फ किसी pattern के हिस्सों को साथ group करने से कहीं ज़्यादा करते हैं -- ये capturing groups बनाते हैं, जो आपको किसी match के खास टुकड़े (जैसे किसी phone number से area code) को पूरे match से अलग निकालने देते हैं, या बाद में एक backreference इस्तेमाल करके उसी pattern के पहले के हिस्से को reference करने देते हैं।
Syntax
javascript
(pattern)          // capturing group
(?:pattern)         // non-capturing
(?<name>pattern)    // named group

Basic Capturing Groups

किसी pattern के हिस्से को parentheses में लपेटना, जैसे (\d{3})-(\d{4}), हर parenthesized हिस्से को अलग-अलग capture करता है -- किसी matching string पर .match() कॉल करना एक array return करता है जहाँ index 0 पूरा match होता है, और हर अगला index क्रम में एक captured group को दिखाता है।

Note: match result array में सही तरीके से index करने के लिए, capturing groups को मन में उनके opening parenthesis के दिखने के क्रम में, बाएं से दाएं, नंबर दें।
Warning: यह भूल जाना कि match()[0] पूरा match है (पहला captured group नहीं), results पढ़ते समय एक बहुत आम off-by-one गलती है।

उदाहरण: Basic Capturing Groups

javascript
const match = "555-1234".match(/(\d{3})-(\d{4})/);
console.log(match[0], match[1], match[2]);

Non-Capturing Groups: (?:...)

एक non-capturing group, जिसे (?:pattern) लिखा जाता है, किसी pattern के हिस्से को group करता है (किसी quantifier को लागू करने के लिए, या alternation को organize करने के लिए) बिना captured results की array में कोई entry जोड़े -- यह तब उपयोगी है जब आपको grouping सिर्फ structure के लिए चाहिए, उस टुकड़े को अलग से निकालने के लिए नहीं।

Note: जब भी कोई group सिर्फ structural grouping के लिए मौजूद हो (जैसे किसी alternation पर एक quantifier लागू करना) और उसके content को individually निकालने की ज़रूरत न हो, तो (?:...) इस्तेमाल करें।
Warning: हर जगह एक regular capturing group इस्तेमाल करना, भले ही captured value कभी इस्तेमाल न हो, बेवजह match result array को unused entries से भर देता है।

उदाहरण: Non-Capturing Groups: (?:...)

javascript
const match = "abcabc".match(/(?:abc)+/);
console.log(match[0]); // grouped for the +, but not captured separately

Named Capturing Groups: (?<name>...)

किसी captured group को उसकी numeric position से reference करने के बजाय, (?<name>pattern) उसे एक readable नाम देता है, जो बाद में match.groups.name से access होता है -- यह कई captured टुकड़ों के साथ काम करने वाले code को numbered positions पर निर्भर रहने से काफी ज़्यादा साफ बनाता है।

Note: जब भी कोई pattern कई टुकड़े capture करे, numeric indices के बजाय named groups को prefer करें, क्योंकि match.groups.year, match[1] से कहीं ज़्यादा साफ पढ़ने में आता है।
Warning: numeric indices पर निर्भर किसी pattern में groups को दोबारा नंबर देना या क्रम बदलना उन indices के हर reference को update करने की मांग करता है -- named groups इस नाज़ुकपन से पूरी तरह बचते हैं।

उदाहरण: Named Capturing Groups: (?<name>...)

javascript
const match = "2024-06-15".match(/(?<year>\d{4})-(?<month>\d{2})/);
console.log(match.groups.year, match.groups.month);

Backreferences: एक Captured Group को दोबारा इस्तेमाल करना

एक backreference, जिसे \1 (या named group के लिए \k<name>) लिखा जाता है, उसी pattern के किसी पिछले group ने असल में जो मैच किया उसे वापस reference करता है -- यह repeated content ढूंढने के लिए उपयोगी है, जैसे लगातार दो बार आने वाला कोई शब्द, बिना यह hardcode किए कि वह repeated content असल में क्या है।

Note: एक backreference का इस्तेमाल खासतौर पर तब करें जब आपको "वही चीज़ फिर से" मैच करनी हो, बिना पहले से जाने कि वह खास text क्या होगा।
Warning: एक backreference ठीक वही text मैच करता है जो group ने इस खास match प्रयास के दौरान capture किया -- यह कोई तय pattern नहीं है, और एक ही string में अलग-अलग matches में अलग-अलग backreferenced content हो सकता है।

उदाहरण: Backreferences: Reusing a Captured Group

javascript
console.log(/(\w+) \1/.test("hello hello")); // true, same word repeated

एक Practical Example: एक पूरा नाम Parse करना

named capturing groups को एक realistic pattern के साथ मिलाना -- किसी पूरे नाम की string से एक first और last name निकालना, या किसी URL को उसके component हिस्सों में तोड़ना -- दिखाता है कि groups कैसे एक single matched pattern को कई individually usable extracted data के टुकड़ों में बदल देते हैं।

Note: capturing groups को उन असली data के टुकड़ों के इर्द-गिर्द design करें जिन्हें आपको बाद में अलग से इस्तेमाल करना है, इस बात के इर्द-गिर्द नहीं कि pattern visually संयोगवश टुकड़ों में कैसे टूटता है।
Warning: एक rigid, तय structure मान लेने वाला parsing pattern (जैसे हमेशा ठीक "First Last") उस real-world input पर fail हो जाएगा जो उस exact मान्यता को follow नहीं करता, जैसे कोई middle name या एक-शब्द का नाम।

उदाहरण: A Practical Example: Parsing a Full Name

javascript
const match = "Sam Sharma".match(/(?<first>\w+) (?<last>\w+)/);
console.log(match.groups.first, match.groups.last);
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. यह भूल जाना कि match() के result array में full match index 0 पर होता है, captured groups index 1 से शुरू होते हैं, 0 से नहीं।
  2. किसी capturing group का इस्तेमाल करना जब एक non-capturing group (?:...) ज़्यादा उपयुक्त होता, match result को unwanted extra captured pieces से भर देते हुए।
  3. किसी pattern में कई groups होने पर backreference numbering को मिला देना, गलत group number को refer करते हुए।
चैप्टर सारांश
  • (pattern) एक capturing group बनाता है, match के उस हिस्से को अलग से extract करते हुए।
  • (?:pattern) एक non-capturing group बनाता है, जो captured results में एक entry जोड़े बिना grouping के लिए उपयोगी है।
  • (?<name>pattern) एक named capturing group बनाता है, जो साफ code के लिए numeric position के बजाय नाम से accessible है।
ब्राउज़र सपोर्ट

Capturing groups को JavaScript regex के सबसे शुरुआती implementation से support मिला है; named capturing groups सभी current major browsers में supported हैं।

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.