Skip to content

Commit 90439e5

Browse files
committed
Add Regex.html with examples of regular expressions and their methods in JavaScript
1 parent 15f937f commit 90439e5

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Example/Lectures/07 Day/Regex.html

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
8+
<script>
9+
10+
// Regex in JavaScript
11+
// Regular expressions are patterns used to match character combinations in strings.
12+
13+
// example of a simple regex
14+
let pattern = /abc/;
15+
let regex = new RegExp("abc");
16+
console.log(pattern.test("Abcdef"));
17+
18+
// Flags
19+
20+
// i - case insensitive
21+
let pattern1 = /abc/i;
22+
console.log(pattern1.test("AbcDef"));
23+
24+
// g - global search
25+
let pattern2 = /abc/g;
26+
let str = "abc abc abc hello abc";
27+
console.log(str.match(pattern2));
28+
29+
// m - multiline
30+
let pattern3 = /^abc/m;
31+
let str2 = "abc\nabc\nabc";
32+
console.log(str2.match(pattern3));
33+
34+
// d - digit
35+
let pattern4 = /\d/;
36+
let str3 = "abc123";
37+
console.log(str3.match(pattern4));
38+
39+
// s - whitespace
40+
let pattern5 = /\s/;
41+
let str4 = "abc def";
42+
console.log(str4.match(pattern5));
43+
44+
// Regex Methods in JavaScript
45+
// test() - tests for a match in a string, returns true or false
46+
let pattern6 = /abc/;
47+
console.log(pattern6.test("abc def")); // true
48+
49+
// exec() - executes a search for a match in a string, returns an array or null
50+
let result = pattern6.exec("abc def abc abc abc abc");
51+
console.log(result);
52+
53+
// match() - retrieves the matches when matching a string against a regex
54+
let str5 = "abc def abc abc abc abc";
55+
let matches = str5.match(pattern6);
56+
console.log(matches);
57+
58+
// replace() - replaces matched substrings with a new substring
59+
let str6 = "abc def abc abc abc abc";
60+
let newStr = str6.replace(pattern6, "xyz");
61+
console.log(newStr);
62+
63+
// search() – Returns index of first match
64+
let index = str6.search(pattern6);
65+
console.log(index);
66+
67+
// split() - splits a string into an array of substrings
68+
let str7 = "abc def abc abc abc abc";
69+
let parts = str7.split(" ");
70+
console.log(parts);
71+
72+
// Validate Email
73+
let email = "test@example.com";
74+
let emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
75+
console.log(emailRegex.test(email));
76+
// Validate Phone Number
77+
let phonePattern = /^\d{10}$/;
78+
console.log(phonePattern.test("9876543210")); // true
79+
80+
// Remove Extra Spaces
81+
let text = " Hello World! ";
82+
let trimmedText = text.replace(/\s+/g, ' ').trim();
83+
console.log(trimmedText); // "Hello World!"
84+
85+
86+
</script>
87+
88+
</head>
89+
<body>
90+
91+
</body>
92+
</html>

0 commit comments

Comments
 (0)