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

JS रेगुलर एक्सप्रेशन्स

एक regular expression टेक्स्ट ढूंढने के लिए एक pattern है, जैसे एक stencil जो सिर्फ कुछ खास shapes से मैच करता है। आप इसे किसी स्ट्रिंग के हिस्सों को search, check, या replace करने के लिए इस्तेमाल करते हैं।
Syntax
javascript
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?

javascript
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

javascript
// 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

javascript
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

javascript
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

javascript
// 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)));
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(/a/, "b") में g flag भूल जाना, इसलिए सिर्फ पहला match बदलता है।
  2. g flag वाले regex को test() के साथ दोबारा इस्तेमाल करना, जहाँ lastIndex results को true और false के बीच alternate करवा देता है।
  3. . या ? जैसे special characters को escape करना भूल जाना, इसलिए /a.b/ a-b को भी match कर लेता है।

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.