-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregular-expressions.js
More file actions
157 lines (126 loc) · 2.55 KB
/
Copy pathregular-expressions.js
File metadata and controls
157 lines (126 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Regex is a sequence of characters that defines a specific search pattern
//Two ways to Create RegEx
const re1 = /ab+c/;
const re2 = new RegExp("ab+c");
// Importance Methods
const text = "My name is partha Sarker.I am 23 years old."
// test method
console.log("test : ",re1.test("abc"));
// match method
console.log("match : ",text.match(/is/));
// replace method
console.log("replace : ",text.replace(/23/g, "24"));
// search method
console.log("search : ",text.search(/name/));
// (.) Any single character
const regex1 = /c.t/;
const words1 = [
"cat",
"cut",
"cot",
"c9t",
"cart",
"ct"
];
// words1.forEach(word => {
// console.log(word, "=>", regex1.test(word));
// });
// (*) Previous character 0 or more times
const regex2 = /ab*/;
const words2 = [
"a",
"ab",
"abb",
"abbbbb",
"xc"
];
// words2.forEach(word => {
// console.log(word, "=>", regex2.test(word));
// });
// (+) Previous character one or more times
const regex3 = /ab+/;
const words3 = [
"a",
"ab",
"abb",
"abbbbb",
"ac"
];
// words3.forEach(word => {
// console.log(word, "=>", regex3.test(word));
// });
// (?) Previous character optional
const regex4 = /colou?r/;
const words4 = [
"color",
"colour",
"colouur",
"colr"
];
// words4.forEach(word => {
// console.log(word, "=>", regex4.test(word));
// });
// [] Match any one character inside []
const regex5 = /gr[ae]y/;
const words5 = [
"gray",
"grey",
"griy",
"gruy"
];
// words5.forEach(word => {
// console.log(word, "=>", regex5.test(word));
// });
// [^] Not these characters
const regex6 = /[^0-9]/;
const words6 = [
"1",
"5",
"a",
"@"
];
// words6.forEach(word => {
// console.log(word, "=>", regex6.test(word));
// });
// | OR
const regex7 = /cat|dog/;
const words7 = [
"cat",
"dog",
"bird",
"cow"
];
// words7.forEach(word => {
// console.log(word, "=>", regex7.test(word));
// });
// () Group
const regex8 = /(Mr|Mrs)\.?\s[A-Z][a-z]+/;
const names = [
"Mr John",
"Mrs Anna",
"Miss Rose",
"Mr. David"
];
names.forEach(name => {
console.log(name, "=>", regex8.test(name));
});
// ^ Start of string
const regex9 = /^Hello/;
const texts = [
"Hello World",
"Hi Hello",
"Hello JS"
];
// texts.forEach(text => {
// console.log(text, "=>", regex9.test(text));
// });
// $ End of string
const regex10 = /JS$/;
const texts2 = [
"Learn JS",
"JS Developer",
"I love JS"
];
// texts2.forEach(text => {
// console.log(text, "=>", regex10.test(text));
// });