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

JS RegExp मेटाकैरेक्टर्स

Metacharacters किसी regex pattern के अंदर ऐसे symbols हैं जिनका खास मतलब होता है, वे खुद literally मैच नहीं करते -- . किसी भी character से मैच करता है, ^ शुरुआत पर anchor करता है, $ अंत पर anchor करता है, और | का मतलब है 'या'। किसी भी non-trivial pattern को पढ़ने या लिखने के लिए इन building blocks को जानना ज़रूरी है।
Syntax
javascript
.   ^   $   \b   \B   |   \

Dot: किसी भी Character से Match करना

. डिफ़ॉल्ट रूप से एक newline को छोड़कर किसी भी single character से मैच करता है -- c.t "cat", "cot", "c8t", या literally c से शुरू और t पर खत्म होने वाले किसी भी three-character sequence से मैच करता है, जो शक्तिशाली है पर अगर आपका मतलब असल में एक खास character था तो ज़्यादा व्यापक हो सकता है।

Note: जब भी आप खासतौर पर एक literal period character मैच करना चाहें, जैसे किसी file extension या decimal point में, तो dot को \. के रूप में escape करें।
Warning: एक literal period मैच करने के इरादे वाले pattern में एक un-escaped . (जैसे किसी file extension को मैच करना) असल में उस position पर किसी भी character से मैच करेगा, सिर्फ एक period से नहीं।

उदाहरण: The Dot: Matching Any Character

javascript
console.log(/c.t/.test("cat")); // true
console.log(/c.t/.test("c8t")); // true, . matches any character

Anchors: ^ और $

^ एक match को string (या m के साथ line) की बिल्कुल शुरुआत पर anchor करता है, और $ बिल्कुल अंत पर -- दोनों को मिलाकर, ^pattern$ को पूरी string का ठीक-ठीक pattern से मैच होना ज़रूरी है, सिर्फ कहीं अंदर उसे contain करना काफी नहीं।

Note: जब भी आपको validate करना हो कि एक पूरा input ठीक-ठीक किसी pattern से मैच करता है, न कि सिर्फ कहीं एक matching substring रखता है, तो ^pattern$ को साथ इस्तेमाल करें।
Warning: यह मतलब रखते हुए कि "पूरी string मैच होनी चाहिए", दो anchors में से एक भूल जाना किसी pattern को ऐसी string पर सफल होने देता है जो कहीं अंदर सिर्फ आंशिक रूप से मैच करती है।

उदाहरण: Anchors: ^ and $

javascript
console.log(/^cat$/.test("cat"));      // true, exact match
console.log(/^cat$/.test("concatenate")); // false, must match entire string

| से Alternation

| किसी pattern के अंदर "or" का मतलब रखता है -- cat|dog "cat" या "dog" दोनों में से किसी से मैच करता है, और grouping parentheses के साथ मिलाकर, gr(a|e)y उस एक position में दोनों में से कोई character allow करके "gray" और "grey" दोनों से मैच करता है।

Note: जब alternation को किसी बड़े pattern के सिर्फ एक हिस्से पर लागू होना चाहिए, | के दोनों तरफ के पूरे pattern पर नहीं, तो इसे parentheses में लपेटें।
Warning: Un-grouped alternation पूरे pattern में | के दोनों तरफ हर चीज़ पर लागू होता है, जो किसी alternation के इरादे से कहीं ज़्यादा व्यापक तरीके से मैच होने का एक आम कारण है।

उदाहरण: Alternation with |

javascript
// Print `/cat|dog/.test("I have a dog")` to the console
// Print `/cat|dog/.test("I have a dog")` to the console
console.log(/cat|dog/.test("I have a dog"));
// Print `/gr(a|e)y/.test("grey")` to the console
// Print `/gr(a|e)y/.test("grey")` to the console
console.log(/gr(a|e)y/.test("grey"));
// Print `/gr(a|e)y/.test("gray")` to the console
// Print `/gr(a|e)y/.test("gray")` to the console
console.log(/gr(a|e)y/.test("gray"));

Special Characters को Escape करना

किसी भी metacharacter (. ^ $ | ? * + ( ) [ ] { } \) को अपने special regex मतलब के बजाय एक literal character की तरह treat होने के लिए उससे पहले एक backslash चाहिए -- यह तब ज़रूरी है जब आपके pattern को इनमें से किसी symbol को असली text के रूप में मैच करना हो।

Note: किसी user-supplied string से एक regex pattern बनाते समय, पहले उसमें हर special character escape करें, वरना user का input अनजाने में pattern की structure बदल सकता है।
Warning: आपके मैच करने वाले text में literally आने वाले किसी special character को escape करना भूल जाना (जैसे किसी price में $, या किसी domain name में .) pattern को अनपेक्षित रूप से व्यवहार कराता है।

उदाहरण: Escaping Special Characters

javascript
console.log(/3\.14/.test("3.14"));  // true, literal dot
console.log(/3\.14/.test("3x14"));  // false

\b से Word Boundaries

\b किसी word character और non-word character के बीच की एक position से मैच करता है (या string की शुरुआत/अंत), बिना खुद किसी character से मैच किए -- \bcat\b standalone शब्द cat से मैच करता है पर category या scatter के हिस्से के रूप में cat से नहीं।

Note: जिस शब्द को आप खासतौर पर एक पूरे शब्द के रूप में मैच करना चाहते हैं उसके आसपास \b इस्तेमाल करें, लंबे शब्दों के अंदर जो सिर्फ वह substring रखते हैं उनमें गलती से होने वाले matches से बचते हुए।
Warning: किसी छोटे शब्द को मैच करते समय \b भूल जाना किसी लंबे शब्द के अंदर एक अनपेक्षित match का एक आम कारण है जिसमें संयोगवश वही substring है।

उदाहरण: Word Boundaries with \b

javascript
console.log(/\bcat\b/.test("I have a cat"));   // true, whole word
console.log(/\bcat\b/.test("category"));       // false, part of a longer word
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. यह भूल जाना कि किसी metacharacter को backslash से escape करना ज़रूरी है (जैसे literal period के लिए \.) जब आप असल में उस exact character को match करना चाहते हैं, उसके special meaning को नहीं।
  2. . को literal period match करने की उम्मीद से इस्तेमाल करना, जबकि यह escape न होने पर असल में कोई भी single character match करता है।
  3. सही ढंग से group करने के लिए parentheses के बिना | (alternation) रखना, जिससे यह intended हिस्से के बजाय पूरे pattern पर लागू हो जाता है।
चैप्टर सारांश
  • . किसी भी single character को match करता है (डिफ़ॉल्ट रूप से newline को छोड़कर)।
  • ^ और $ किसी match को क्रमशः string (या m flag के साथ line) की शुरुआत और अंत से anchor करते हैं।
  • | का मतलब है "या", पहले या बाद वाले pattern में से किसी एक को match करना।
ब्राउज़र सपोर्ट

सभी standard regex metacharacters हर 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.