JS RegExp मेटाकैरेक्टर्स
In this page:
. ^ $ \b \B | \
Dot: किसी भी Character से Match करना
. डिफ़ॉल्ट रूप से एक newline को छोड़कर किसी भी single character से मैच करता है -- c.t "cat", "cot", "c8t", या literally c से शुरू और t पर खत्म होने वाले किसी भी three-character sequence से मैच करता है, जो शक्तिशाली है पर अगर आपका मतलब असल में एक खास character था तो ज़्यादा व्यापक हो सकता है।
उदाहरण: The Dot: Matching Any Character
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 करना काफी नहीं।
उदाहरण: Anchors: ^ and $
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" दोनों से मैच करता है।
उदाहरण: Alternation with |
// 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 के रूप में मैच करना हो।
उदाहरण: Escaping Special Characters
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 से नहीं।
उदाहरण: Word Boundaries with \b
console.log(/\bcat\b/.test("I have a cat")); // true, whole word
console.log(/\bcat\b/.test("category")); // false, part of a longer word
- यह भूल जाना कि किसी metacharacter को backslash से escape करना ज़रूरी है (जैसे literal period के लिए \.) जब आप असल में उस exact character को match करना चाहते हैं, उसके special meaning को नहीं।
- . को literal period match करने की उम्मीद से इस्तेमाल करना, जबकि यह escape न होने पर असल में कोई भी single character match करता है।
- सही ढंग से 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 से इसका हिस्सा रहे हैं।
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