JS - RegExp
Introduction for Regular Expression
हर Programming Language में Regular Expression का जिक्र होता है | Regular Expression ये String को match करने का search pattern होता है | Web Development के Languages में देखे तो Form के Validation में Regular Expression का इस्तेमाल होता है | जैसे Email, UserName, Mobile No के validation के लिए Regular Expression का इस्तेमाल होता है |
Syntax for Creating Regular Expression By Literal
//Syntax var regex = regular_expression/flag(s) //Example var regex = /[0-9]/g; //or var regex = "[0-9]" can't use flags;
Syntax for Creating Regular Expression By Object Constructor
//Syntax var regex = new RegExp(regular_expression, flag(s)) //Example var regex = new RegExp(/[0-9]/, "g"); //or var regex = new RegExp("[0-9]", "g");
regular_expression : यहाँ पर regular expression दिया जाता है |
flag(s) : यहाँ पर match करने के लिए g(global), i(case-insensitive/ignoreCase) और m(multiline) ये Modifiers दिए जाते है |
Javascript में Regular Expression के लिए कुछ हिस्से बनाये गए है |
- Brackets
- Quantifiers
- Metacharacters
- Modifiers
String Methods For Regular Expression
String Method | Description |
---|---|
match() | Regular Expression या seperator के जरिये String को split करके match हुए content को return किया जाता है | |
replace() | Regular Expression के जरिये String को search करके match हुए content को दिए हुए value से replace किया जाता है | |
search() | Regular Expression के जरिये String को search करके match हुए first occurrence का index return करता है | |
split() | Regular Expression या seperator के जरिये String को split करके match हुए content को return किया जाता है | |
1. Brackets
Expressions | Description |
---|---|
[..] | कोई भी character या digits ढूंढने के लिए दिया जाता है | |
[^..] | कोई भी character या digits दिया जाता है , उसे ढूंढा नहीं जाता | |
[0-9] | 0 से 9 तक के digits को ढूंढा जाता है | |
[^0-9] | 0 से 9 तक के digits को ढूंढा नहीं जाता है | |
[A-Z] | Uppercase A से लेकर uppercase Z तक characters को ढूंढा जाता है | |
[^A-Z] | Uppercase A से लेकर uppercase Z तक characters को ढूंढा नहीं जाता है | |
[a-z] | lowercase a से लेकर lowercase z तक characters को ढूंढा जाता है | |
[^a-z] | lowercase a से लेकर lowercase z तक characters को ढूंढा नहीं जाता है | |
2. Quantifiers
Expressions | Description |
---|---|
n+ | String में कम से कम एक 'n' को match होता है | |
n* | String में zero या उससे ज्यादा 'n' occurrences से match होता है | |
n? | String में zero या एक 'n' occurrences से match होता है | |
n{X} | String में से n को sequences of X number तक match किया जाता है | |
n{X, Y} | String में से n को sequences of X number से Y number तक match किया जाता है | |
n{X,} | String में से n को sequences of कम से कम X number तक किया जाता है | |
n$ | String में से n को end पर match किया जाता है | |
^n | String में से n को start पर match किया जाता है | |
3. Metacharacters
Expressions | Description |
---|---|
dot(.) | single dot से single character ढूंढा जाता है | |
b | String में से word के शुरुआत या end के matches ढूंढे जाते है | |
B | String में से word के ढूंढे हुए matches शुरुआत या end पर नहीं होते है | |
d | String में से 0 से 9 numbers तक ढूंढा जाता है | |
D | String में से non-numbers character ढूंढा जाता है | |
f | String में से form-feed character को ढूंढा जाता है | |
n | String में से newline character को ढूंढा जाता है | |
r | String में से carriage return character को ढूंढा जाता है | |
s | String में से whitespace character को ढूंढा जाता है | |
S | String में से non-whitespace character को ढूंढा जाता है | |
t | String में से tab character को ढूंढा जाता है | |
v | String में से vertical tab character को ढूंढा जाता है | |
w | String में से word character को ढूंढा जाता है | |
W | String में से non-word character को ढूंढा जाता है | |
0 | String में से null character को ढूंढा जाता है | |
4. Modifiers
Modifiers Regular Expressions के साथ आखिर में इस्तेमाल किये जाते है | ये Regular Expression को modify करने में मदद करते है |
Flags | Description |
---|---|
g(global) | एक से ज्यादा matches के लिए इस्तेमाल किया जाता है | |
m(multiline) | हर line के matches के लिए इस्तेमाल किया जाता है | |
i(case-insensitive) | case-insensitive matches के लिए इस्तेमाल किया जाता है | |
Regular Expression Properties
Properties | Description |
---|---|
global | Regular Expression के साथ 'g' Modifier का इस्तेमाल किया गया है या नहीं ये check किया जाता है | |
ignoreCase | Regular Expression के साथ 'i' Modifier के इस्तेमाल किया गया है या नहीं ये check किया जाता है | |
lastIndex | matches characters की positions return की जाती है | |
multiline | Regular Expression के साथ 'm' Modifier का इस्तेमाल किया गया है या नहीं ये check किया जाता है | |
source | regular expression को return किया जाता है | |
Regular Expression Methods
Methods | Description |
---|---|
exec() | string के matches को ढूंढा जाता है और पहले match को return किया जाता है | |
test() | string के matches को ढूंढा जाता है और boolean value return की जाती है | |
toString() | Regular Expression string return किया जाता है | |