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

JS RegExp फ्लैग्स

किसी regular expression के flags -- वे single letters जो closing slash के बाद रखे जाते हैं, जैसे /pattern/gi -- यह बदलते हैं कि pattern कैसे मैच करता है: case-insensitive तरीके से, सिर्फ पहले के बजाय हर match पर globally, या कई lines में। हर flag को समझना वह matching व्यवहार पाने के लिए ज़रूरी है जो आप असल में चाहते हैं।
Syntax
javascript
/pattern/g   // global
/pattern/i   // case-insensitive
/pattern/m   // multiline
/pattern/gi  // combined

Global Flag (g)

बिना g के, एक regex किसी string में सिर्फ पहला match ढूंढकर रुक जाता है -- g के साथ, .match() और .replace() जैसे methods पूरी string में हर match ढूंढते और उस पर काम करते हैं, सिर्फ पहली occurrence पर नहीं।

Note: जब आप हर occurrence बदलना चाहते हों, सिर्फ पहली मिली को नहीं, तो हमेशा .replace() के साथ g शामिल करें।
Warning: g flag वाला एक regex .test() या .exec() के साथ दोबारा इस्तेमाल होने पर stateful हो जाता है -- यह lastIndex के ज़रिए calls के बीच अपनी position याद रखता है, जो हैरान करने वाले बदलते-रहते results दे सकता है।

उदाहरण: The Global Flag (g)

javascript
console.log("cat cat cat".match(/cat/));   // only the first match
console.log("cat cat cat".match(/cat/g));  // every match

Case-Insensitive Flag (i)

i flag किसी pattern को upper या lower case की परवाह किए बिना मैच कराता है -- /cat/i "cat", "CAT", "Cat", और किसी भी और capitalization से मैच करता है -- यह तब उपयोगी है जब input का exact case भरोसेमंद न हो, जैसे किसी user-entered शब्द को validate करना।

Note: i का इस्तेमाल खासतौर पर तब करें जब case सच में match के लिए मायने नहीं रखनी चाहिए, इसे हर pattern में एक reflexive default के रूप में जोड़ने के बजाय।
Warning: ऐसे pattern में i जोड़ना जिसे खासतौर पर case में फर्क करना है (जैसे किसी case-sensitive code या identifier से मैच करना) match को गलत तरीके से अनचाहे cases तक बढ़ा देगा।

उदाहरण: The Case-Insensitive Flag (i)

javascript
console.log(/cat/i.test("CAT")); // true, case-insensitive

Multiline Flag (m)

डिफ़ॉल्ट रूप से, ^ सिर्फ पूरी string की बिल्कुल शुरुआत से और $ सिर्फ बिल्कुल अंत से मैच करता है, किसी multi-line string में भी -- m flag इसे बदल देता है ताकि ^ और $ इसके बजाय string के अंदर हर individual line की शुरुआत और अंत से मैच करें।

Note: m का इस्तेमाल खासतौर पर तब करें जब आप किसी multi-line string के साथ काम कर रहे हों जहाँ आपको हर line की शुरुआत या अंत में patterns मैच करने हैं, सिर्फ पूरी string में नहीं।
Warning: multi-line text के साथ काम करते समय m flag भूल जाना एक आम कारण है कि ^ या $ anchors बिल्कुल पहली या आखिरी line के अलावा कहीं और उम्मीद के मुताबिक "काम नहीं करते" दिखते हैं।

उदाहरण: The Multiline Flag (m)

javascript
const text = "line one\nline two";
console.log(text.match(/^line/gm)); // matches start of each line

Unicode Flag (u)

u flag पूरी Unicode-aware pattern matching को सक्षम करता है, Basic Multilingual Plane के बाहर के characters (जैसे कई emoji) को सही तरीके से संभालते हुए, जिन्हें इस flag के बिना एक की जगह दो अलग characters के रूप में गलत समझा जा सकता है।

Note: जब किसी pattern को emoji या UTF-16 में surrogate pairs से represent होने वाले दूसरे characters सही तरीके से मैच करने हों, तो u शामिल करें।
Warning: बिना u के, /./ जैसा एक pattern किसी emoji character का सिर्फ आधा मैच कर सकता है, क्योंकि JavaScript strings अंदर से UTF-16 इस्तेमाल करती हैं और कुछ characters दो code units में फैले होते हैं।

उदाहरण: The Unicode Flag (u)

javascript
console.log(/\u{1F600}/u.test("\u{1F600}")); // correctly handles characters outside the basic plane

कई Flags को Combine करना

Flags को closing slash के बाद किसी भी क्रम में combine किया जा सकता है -- /pattern/gi global और case-insensitive matching दोनों एक साथ लागू करता है, /pattern/gm global और multiline को मिलाता है, वगैरह, जो आपको किसी task को ठीक-ठीक चाहिए व्यवहार मिलाने देता है।

Note: सिर्फ वही flags combine करें जो किसी खास pattern को असल में चाहिए -- अनावश्यक flags जोड़ना आमतौर पर कुछ तोड़ता नहीं, पर किसी पढ़ने वाले के लिए intended matching व्यवहार को कम साफ बना सकता है।
Warning: flags किस क्रम में लिखे जाते हैं यह मायने नहीं रखता (gi और ig बिल्कुल एक जैसा व्यवहार करते हैं), पर हर flag सिर्फ एक बार आ सकता है -- किसी flag को दोहराना एक syntax error देता है।

उदाहरण: Combining Multiple Flags

javascript
console.log("Cat cat CAT".match(/cat/gi)); // combines global + case-insensitive
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. .replace() के साथ इस्तेमाल होने वाले pattern में g flag भूल जाना, जो फिर string में हर occurrence के बजाय सिर्फ बहुत पहले match को replace करता है।
  2. lastIndex reset किए बिना कई .test() calls में g flag वाले regex object को दोबारा इस्तेमाल करना, stateful matching position की वजह से alternating true/false results देते हुए।
  3. आदत से i flag जोड़ना भले ही case sensitivity असल में intended behavior हो।
चैप्टर सारांश
  • g (global) किसी string में सभी matches ढूंढता है, सिर्फ पहला नहीं।
  • i (case-insensitive) upper या lower case चाहे जो भी हो match करता है।
  • m (multiline) ^ और $ को पूरी string के बजाय हर line की शुरुआत/अंत से match कराता है।
ब्राउज़र सपोर्ट

सभी standard regex flags (g, i, m, s, u, y) हर modern browser में 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.