-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
101 lines (62 loc) · 3.21 KB
/
index.js
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
console.log("-------------------------------------------------");
console.log("Activity 1: ");
// 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.
let pattern = /JavaScript/g;
let str = "JavaScript is a scripting language. JavaScript is used for web development. JavaScript makes web pages interactive.";
let matches = str.match(pattern);
console.log(matches);
// Task 2: Write a regular expression to match all digits in a string. Log the matches.
pattern = /\d/g;
str = "There are 12 months in a year.";
matches = str.match(pattern);
console.log(matches);
console.log("-------------------------------------------------");
console.log("Activity 2: ");
// Task 3: Write a regular expression to match all words in a string that start with a capital letter. Log the matches.
pattern = /[A-Z][a-z]+/g;
str = "The quick brown fox Jumps over the lazy dog.";
matches = str.match(pattern);
console.log(matches);
// Task 4: Write a regular expression to match all sequences of one or more digits in a string. Log the matches.
pattern = /\d+/g;
str = "There are 12 months in a year.";
matches = str.match(pattern);
console.log(matches);
console.log("-------------------------------------------------");
console.log("Activity 3: ");
// 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.
pattern = /\((\d{3})\)\s(\d{3})-(\d{4})/;
str = "(123) 456-7890";
matches = str.match(pattern);
console.log(matches);
// Task 6: Write a regular expression to capture the username and domain from an email address. Log the captures.
pattern = /(\w+)@(\w+\.\w+)/;
str = "yashsaini" + "@" + "gmail.com";
let email = "yashkumarsaini0123456789@gmail.com";
matches = email.match(pattern);
console.log(matches);
console.log("-------------------------------------------------");
console.log("Activity 4: ");
// Task 7: Write a regular expression to match a word only if it is at the beginning of a string. Log the matches.
pattern = /\b\w+/g;
str = "Hello, World!";
matches = str.match(pattern);
console.log(matches);
// Task 8: Write a regular expression to match a word only if it is at the end of a string. Log the matches.
pattern = /\w+\b/g;
str = "The quick brown fox jumps over the lazy dog.";
matches = str.match(pattern);
console.log(matches);
console.log("-------------------------------------------------");
console.log("Activity 5: ");
// 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.
pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
let password = "Password@123";
let isValid = pattern.test(password);
console.log(isValid);
// Task 10: Write a regular expression to validate a URL. Log whether the URL is valid.
pattern = /^(https?:\/\/)?(www\.)?([a-zA-Z0-9-]+)\.([a-zA-Z]{2,})(\/[a-zA-Z0-9-]+)*\/?$/;
let url = "https://www.google.com";
isValid = pattern.test(url);
console.log(isValid);
console.log("-------------------------------------------------");