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

JS RegExp क्लासेस

Character classes shorthand notations हैं जो एक साथ characters की पूरी categories से मैच करते हैं -- किसी भी digit के लिए \d, किसी भी word character के लिए \w, किसी भी whitespace के लिए \s -- और इनके uppercase विपरीत (\D, \W, \S) उस category में NOT आने वाली हर चीज़ से मैच करते हैं, जो आपको हर individual character लिस्ट किए बिना patterns बनाने देते हैं।
Syntax
javascript
[abc]   [^abc]   [a-z]
\d   \w   \s
\D   \W   \S

\d से Digits

\d किसी भी single digit character से मैच करता है, 0 से 9 तक -- \d\d\d ठीक तीन consecutive digits से मैच करता है, और \d+ (एक quantifier के साथ मिलाकर) लगातार एक या ज़्यादा digits से मैच करता है, जो numbers, phone digits, या zip codes मैच करने की बुनियाद है।

Note: सिर्फ एक digit के बजाय multi-digit numbers मैच करने के लिए \d को quantifiers (+ एक या ज़्यादा के लिए, {n} ठीक n के लिए) के साथ मिलाएं।
Warning: अकेला \d ठीक एक digit से मैच करता है -- किसी multi-digit number को मैच करने का इरादा रखते हुए एक quantifier भूल जाना एक बहुत आम regex गलती है।

उदाहरण: Digits with \d

javascript
console.log(/\d\d\d/.test("123")); // exactly 3 digits
console.log("Phone: 12345".match(/\d+/));

\w से Word Characters

\w किसी भी letter (upper या lower case), digit, या underscore से मैच करता है -- असल में वह characters का सेट जो आमतौर पर किसी variable name या साधारण identifier में valid होता है -- पर खास तौर पर इसमें spaces, hyphens, या ज़्यादातर punctuation शामिल नहीं हैं।

Note: याद रखें \w spaces और ज़्यादातर punctuation को exclude करता है -- अगर आपको spaces वाले किसी पूरे नाम को मैच करना है, तो \w को एक explicit space character के साथ मिलाएं या इसके साथ \s इस्तेमाल करें।
Warning: spaces वाले किसी पूरे sentence या phrase को मैच करने के लिए \w+ इस्तेमाल करना पहले space पर रुक जाता है, क्योंकि spaces \w character class का हिस्सा नहीं हैं।

उदाहरण: Word Characters with \w

javascript
console.log(/^\w+$/.test("user_1"));  // true, letters/digits/underscore
console.log(/^\w+$/.test("user 1"));  // false, contains a space

\s से Whitespace

\s किसी भी whitespace character से मैच करता है -- एक regular space, एक tab, या एक newline -- यह किसी भी तरह के whitespace पर text split करने या असंगत spacing को normalize करने के लिए उपयोगी है, सिर्फ एक literal space character मैच करने के बजाय।

Note: उस text को normalize या split करते समय जिसमें tabs या कई consecutive spaces हो सकते हैं, एक literal space के बजाय \s+ (एक या ज़्यादा whitespace characters) इस्तेमाल करें।
Warning: किसी pattern में एक literal space character सिर्फ एक असली space से मैच करता है -- यह \s की तरह किसी tab या newline से मैच नहीं करेगा, जिससे एक pattern अलग whitespace से formatted text को miss कर सकता है।

उदाहरण: Whitespace with \s

javascript
console.log("a\tb  c".split(/\s/)); // splits on tab and spaces alike

Negated Classes: \D, \W, \S

shorthand classes के uppercase versions अपने lowercase counterparts के ठीक उलट मैच करते हैं -- \D किसी भी non-digit से मैच करता है, \W किसी भी non-word-character से, और \S किसी भी non-whitespace character से।

Note: एक negated class का इस्तेमाल तब करें जब यह describe करना ज़्यादा स्वाभाविक हो कि आप क्या exclude करना चाहते हैं, न कि क्या include करना चाहते हैं।
Warning: \D जैसी एक negated class अभी भी whitespace और punctuation से मैच करती है, सिर्फ letters से नहीं -- यह खासतौर पर सिर्फ digits exclude करती है, बस इतना ही।

उदाहरण: Negated Classes: \D, \W, \S

javascript
console.log(/\D/.test("abc"));  // true, non-digit found
console.log(/\W/.test("a_b"));  // false, only word chars/underscore
console.log(/\S/.test("  "));   // false, only whitespace

[ ] से Custom Character Classes

Square brackets एक custom character class define करते हैं जो दिए गए set से किसी भी single character से मैच करता है -- [aeiou] किसी भी एक vowel से मैच करता है, और [a-z] किसी भी एक lowercase letter से मैच करने के लिए एक range इस्तेमाल करता है, जो आपको built-in shorthand classes से आगे सटीक character sets बनाने देता है।

Note: हर individual character लिस्ट करने के बजाय, किसी लगातार range के अंदर किसी भी character को मैच करने के एक compact तरीके के लिए [a-z] या [0-9] जैसी range इस्तेमाल करें।
Warning: बिना i flag के एक custom character class डिफ़ॉल्ट रूप से case-sensitive होती है -- [a-z] किसी uppercase letter से मैच नहीं करेगा जब तक A-Z न जोड़ा जाए या i flag लागू न किया जाए।

उदाहरण: Custom Character Classes with [ ]

javascript
console.log(/[aeiou]/.test("sky"));  // false, no vowels
console.log(/[a-z]/.test("ABC1"));   // false, no lowercase letters
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. यह भूल जाना कि \w सिर्फ letters, digits, और underscore को match करता है -- इसमें spaces या punctuation शामिल नहीं होते, जो spaces वाले names या usernames validate करते समय एक common surprise है।
  2. [a-z] जैसा custom character class इस्तेमाल करना बिना यह realize किए कि यह डिफ़ॉल्ट रूप से case-sensitive है, जिसके लिए एक explicit A-Z range या i flag चाहिए।
  3. \s (कोई भी whitespace: space, tab, newline) को किसी pattern में एक literal space character से confuse करना जिसे tabs या newlines भी match करने चाहिए।
चैप्टर सारांश
  • \d किसी digit (0-9) को match करता है, \w किसी word character (letters, digits, underscore) को match करता है, और \s किसी whitespace character को match करता है।
  • uppercase versions (\D, \W, \S) बिल्कुल उल्टा match करते हैं -- वह कोई भी character जो उस category में NOT है।
  • Custom character classes, [aeiou] या [a-z] जैसे square brackets में लिखी जाती हैं, specified set या range से किसी भी single character को match करती हैं।
ब्राउज़र सपोर्ट

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