JS रेगुलर एक्सप्रेशन्स
const regex = /pattern/flags;
const regex = new RegExp("pattern", "flags");
regex.test(string);
string.match(regex);
string.replace(regex, replacement);
Regex क्या है?
एक regular expression टेक्स्ट ढूंढने या जांचने के लिए इस्तेमाल होने वाला एक pattern है। Patterns slashes के बीच लिखे जाते हैं, जैसे /abc/, और इनमें g (global) या i (case-insensitive) जैसे flags शामिल हो सकते हैं जो match के व्यवहार को बदलते हैं।
उदाहरण: What Is Regex?
const pattern = /abc/i;
console.log(pattern.test("ABC123")); // true, case-insensitive flag
Regex Methods
JavaScript regular expressions के साथ काम करने के लिए test() और match() देता है। test() एक boolean return करता है जो बताता है कि pattern स्ट्रिंग में कहीं मैच करता है या नहीं, जबकि match() असल में matched substring(s) return करता है या कुछ न मिलने पर null।
उदाहरण: Regex Methods
// Declare the constant `pattern`, set to `/cat/`
// Declare the constant `pattern`, set to `/cat/`
const pattern = /cat/;
// Print `pattern.test("I have a cat")` to the console
// Print `pattern.test("I have a cat")` to the console
console.log(pattern.test("I have a cat"));
// Print `"I have a cat".match(pattern)` to the console
// Print `"I have a cat".match(pattern)` to the console
console.log("I have a cat".match(pattern));
आम Patterns
[a-z] जैसे character classes किसी range के किसी भी character से मैच करते हैं, और + या {2,4} जैसे quantifiers control करते हैं कि पहले वाला हिस्सा कितनी बार दोहरा सकता है। Character classes और quantifiers उपयोगी text patterns बनाने में मदद करते हैं।
उदाहरण: Common Patterns
const pattern = /[a-z]+\d{2,4}/;
console.log(pattern.test("abc123"));
Regex के साथ Replace
replace() matching text बदलने के लिए एक regular expression इस्तेमाल कर सकता है। replace() को (साधारण string के बजाय) एक regex पास करना, g flag सेट होने पर सिर्फ पहली occurrence के बजाय हर match को एक साथ बदलने देता है।
उदाहरण: Replace With Regex
const text = "cat cat cat";
console.log(text.replace(/cat/g, "dog"));
Practice Patterns
साधारण patterns से शुरू करें और अलग-अलग inputs test करें। Regex syntax घना होता है, इसलिए पूरा pattern blind लिखने के बजाय असली sample strings के खिलाफ धीरे-धीरे patterns बनाना गलतियां पकड़ना कहीं आसान बना देता है।
उदाहरण: Practice Patterns
// Declare the constant `patterns`, set to `[/^\d+$/, /[A-Z]/, /\s/]`
// Declare the constant `patterns`, set to `[/^\d+$/, /[A-Z]/, /\s/]`
const patterns = [/^\d+$/, /[A-Z]/, /\s/];
// Declare the constant `input`, set to "Test 123"
// Declare the constant `input`, set to "Test 123"
const input = "Test 123";
// Call `patterns.forEach(p => console.log(p, p.test(input)))`
// Call `patterns.forEach(p => console.log(p, p.test(input)))`
patterns.forEach(p => console.log(p, p.test(input)));
replace(/a/, "b")मेंgflag भूल जाना, इसलिए सिर्फ पहला match बदलता है।gflag वाले regex कोtest()के साथ दोबारा इस्तेमाल करना, जहाँlastIndexresults को true और false के बीच alternate करवा देता है।.या?जैसे special characters को escape करना भूल जाना, इसलिए/a.b/a-bको भी match कर लेता है।
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