Skip to content

Commit ae26e7f

Browse files
committed
Day 19
1 parent aa3a392 commit ae26e7f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Diff for: Day 19 regular exp.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Activity 1: Basic Regular Expressions
2+
//Task 1: Write a regular expression to match a simple pattern (e.g., match all occurrences of the word "JavaScript" in a string). Log the matches.
3+
4+
const text1 = "JavaScript is a versatile language. JavaScript can be used for both front-end and back-end development.";
5+
const pattern1 = /JavaScript/g;
6+
7+
const matches1 = text1.match(pattern1);
8+
console.log("Matches for Task 1:", matches1);
9+
10+
11+
//Task 2: Write a regular expression to match all digits in a string. Log the matches
12+
13+
const text2 = "The year 2024 marks a new era. My phone number is 123-456-7890.";
14+
const pattern2 = /\d+/g;
15+
16+
const matches2 = text2.match(pattern2);
17+
console.log("Matches for Task 2:", matches2);
18+
19+
20+
// Activity 2: Character Classes and Quantifiers
21+
//Task 3: Write a regular expression to match all words in a string that start with a capital letter. Log the matches.
22+
23+
const text3 = "JavaScript is a versatile language. The Year 2024 marks a New Era.";
24+
const pattern3 = /\b[A-Z][a-z]*\b/g;
25+
26+
const matches3 = text3.match(pattern3);
27+
console.log("Matches for Task 3:", matches3);
28+
29+
30+
//Task 4: Write a regular expression to match all sequences of one or more digits in a string. Log the matches.
31+
const text4 = "There are 365 days in a year. My phone number is 123-456-7890.";
32+
const pattern4 = /\d+/g;
33+
34+
const matches4 = text4.match(pattern4);
35+
console.log("Matches for Task 4:", matches4);
36+
37+
// Activity 3: Grouping and Capturing
38+
//Task 5: Write a regular expression to capture the area code, central office code, and line number from a US phone number format (e.g., (123) 456-7890). Log the captures.
39+
40+
const text5 = "My phone number is (123) 456-7890.";
41+
const pattern5 = /\((\d{3})\)\s(\d{3})-(\d{4})/;
42+
43+
const matches5 = text5.match(pattern5);
44+
console.log("Matches for Task 5:", matches5);
45+
46+
47+
//Task 6: Write a regular expression to capture the username and domain from an email address. Log the captures.
48+
49+
const text6 = "Contact me at [email protected].";
50+
const pattern6 = /(\w+)@(\w+\.\w+)/;
51+
52+
const matches6 = text6.match(pattern6);
53+
console.log("Matches for Task 6:", matches6);
54+
55+
56+
// Activity 4: Assertions and Boundaries
57+
//Task 7: Write a regular expression to match a word only if it is at the beginning of a string. Log the matches.
58+
const text7 = "JavaScript is a versatile language.";
59+
const pattern7 = /^\w+/;
60+
61+
const matches7 = text7.match(pattern7);
62+
console.log("Matches for Task 7:", matches7);
63+
64+
65+
//Task 8: Write a regular expression to match a word only if it is at the end of a string. Log the matches.
66+
67+
const text8 = "JavaScript is a versatile language";
68+
const pattern8 = /\w+$/;
69+
70+
const matches8 = text8.match(pattern8);
71+
console.log("Matches for Task 8:", matches8);
72+
73+
74+
// Activity 5: Practical Applications
75+
//Task 9: Write a regular expression to validate a simple password (must include at least one uppercase letter, one lowercase letter, one digit, and one special character). Log whether the password is valid.
76+
const password = "P@ssw0rd";
77+
const pattern9 = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
78+
79+
const isValidPassword = pattern9.test(password);
80+
console.log("Is the password valid?", isValidPassword);
81+
82+
//Task 10: Write a regular expression to validate a URL. Log whether the URL is valid.
83+
84+
const url = "https://www.example.com";
85+
const pattern10 = /^(https?:\/\/)?(www\.)?([a-zA-Z0-9]+)(\.[a-zA-Z]{2,})(\/\w*)*\/?$/;
86+
87+
const isValidURL = pattern10.test(url);
88+
console.log("Is the URL valid?", isValidURL);

0 commit comments

Comments
 (0)