Related Topics
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36

JAVASCRIPT
function validatePassword(password) {
// Regular expression pattern to validate the password
const passwordPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d!@#$%^&*()-_=+{}[\]|;:'",./?]{8,}$/;
return passwordPattern.test(password);
}
// Test the function with some example passwords
console.log(validatePassword("Abc12345")); // Output: true
console.log(validatePassword("password")); // Output: false (no uppercase)
console.log(validatePassword("12345678")); // Output: false (no uppercase and lowercase)
console.log(validatePassword("Abcdefgh")); // Output: false (no digit)
console.log(validatePassword("Abc@1234")); // Output: true
Explanation of the regular expression pattern:
^
: Matches the start of the string.(?=.*[a-z])
: Positive lookahead assertion for at least one lowercase letter.(?=.*[A-Z])
: Positive lookahead assertion for at least one uppercase letter.(?=.*\d)
: Positive lookahead assertion for at least one digit.[a-zA-Z\d!@#$%^&*()-_=+{}[\]|;:'",.<>/?]{8,}
: Matches the actual password content, ensuring it contains at least 8 characters and can include any combination of letters (uppercase and lowercase), digits, and special characters from the provided set.$
: Matches the end of the string.
The test()
method of the regular expression is used to check if the password matches the pattern. It returns true
if the password meets all the specified criteria and false
otherwise.
You can customize the regular expression pattern according to your specific password requirements, such as enforcing a specific set of special characters or other rules. Regular expressions provide a flexible way to validate complex patterns like passwords in a concise and powerful manner.
const text = "The quick brown fox jumps over the lazy fox.";
const wordToReplace = "fox";
const replacement = "cat";
// Create a regular expression with the 'g' flag to match all occurrences of the word
const regex = new RegExp(wordToReplace, "g");
// Use the 'replace()' method to replace all instances of the word with the replacement
const replacedText = text.replace(regex, replacement);
console.log(replacedText);
// Output: "The quick brown cat jumps over the lazy cat."
In this example, we have a string text
containing the sentence “The quick brown fox jumps over the lazy fox.” We want to replace all occurrences of the word “fox” with the word “cat”.
Here’s how the code works:
We define the
wordToReplace
andreplacement
variables to store the word to be replaced and the replacement word, respectively.We construct a regular expression
regex
using theRegExp
constructor with theg
flag to make sure it matches all occurrences of the word in the text.We use the
replace()
method on thetext
string, passing in theregex
and thereplacement
as arguments. The method replaces all occurrences of the word “fox” with “cat”.The modified string is stored in the
replacedText
variable, which we then log to the console.
Note that the replace()
method does not modify the original string; instead, it returns a new string with the replacements applied. If you want to modify the original string, you can reassign the text
variable to the replacedText
.
const text = "Today is 2023-07-15, and it's a sunny day.";
const dateRegex = /(\d{4}-\d{2}-\d{2})/;
const match = text.match(dateRegex);
if (match) {
const extractedDate = match[1];
console.log("Extracted date:", extractedDate);
} else {
console.log("Date not found!");
}
// Output: "Extracted date: 2023-07-15"
Non-Capturing Groups: Non-capturing groups, denoted by
(?:...)
, are similar to capturing groups but are used when you want to match a pattern without capturing it. This can be helpful to improve performance or when you’re interested in a specific pattern but don’t want it in the result.
Example: Matching URLs without capturing the protocol
const text = "Visit our website at https://www.example.com";
const urlRegex = /(?:https?:\/\/)?(www\.[\w-]+\.[a-z]+)/;
const match = text.match(urlRegex);
if (match) {
const extractedURL = match[1];
console.log("Extracted URL:", extractedURL);
} else {
console.log("URL not found!");
}
// Output: "Extracted URL: www.example.com"
Lookaheads and Lookbehinds: Lookaheads and lookbehinds are assertions that don’t consume characters during the match, allowing you to check for patterns before or after the main match.
Example: Extracting email addresses using a positive lookahead
const text = "Please contact support@example.com for assistance.";
const emailRegex = /\b\w+@\w+\.\w+(?=\b)/;
const match = text.match(emailRegex);
if (match) {
const extractedEmail = match[0];
console.log("Extracted email:", extractedEmail);
} else {
console.log("Email not found!");
}
// Output: "Extracted email: support@example.com"
String methods with regular expressions: Besides
match()
, you can use other string methods likesearch()
,split()
,replace()
, andexec()
with regular expressions to extract information from strings. Each method has its specific use case and benefits.
Remember to thoroughly test your regular expressions to handle different cases and ensure accurate extraction of information. Regular expressions can be very powerful, but they can also be complex, so understanding them and testing them thoroughly is essential to avoid unexpected results.
const text = "I love apple pie and apple cider.";
const regex = /apple(?= pie)/g;
const matches = text.match(regex);
console.log(matches); // Output: ["apple"]
In this example, the regex /apple(?= pie)/
matches the word “apple” only if it is followed by the word “pie.” The (?= pie)
part is the positive lookahead, which checks if “pie” comes after “apple” without including “pie” in the actual match.
Negative Lookahead Assertion
(?!...)
: A negative lookahead assertion is denoted by(?!...)
. It asserts that a certain pattern does not exist after the current position.
Example: Matching the word “apple” only if it is not followed by “cider.”
const text = "I love apple pie and apple cider.";
const regex = /apple(?! cider)/g;
const matches = text.match(regex);
console.log(matches); // Output: ["apple"]
In this example, the regex /apple(?! cider)/
matches the word “apple” only if it is not followed by the word “cider.” The (?! cider)
part is the negative lookahead, which ensures that “cider” does not come after “apple” without including “cider” in the actual match.
Positive Lookbehind Assertion
(?<=...)
: A positive lookbehind assertion is denoted by(?<=...)
. It asserts that a certain pattern exists before the current position without including it in the match.
Example: Matching the word “pie” only if it is preceded by “apple.”
const text = "I love apple pie and cherry pie.";
const regex = /(?<=apple )pie/g;
const matches = text.match(regex);
console.log(matches); // Output: ["pie"]
In this example, the regex /(?<=apple )pie/
matches the word “pie” only if it is preceded by the word “apple” and a space. The (?<=apple )
part is the positive lookbehind, which checks if “apple ” comes before “pie” without including “apple ” in the actual match.
Negative Lookbehind Assertion
(?<!...)
: A negative lookbehind assertion is denoted by(?<!...)
. It asserts that a certain pattern does not exist before the current position.
Example: Matching the word “pie” only if it is not preceded by “cherry.”
const text = "I love apple pie and cherry pie.";
const regex = /(?<!cherry )pie/g;
const matches = text.match(regex);
console.log(matches); // Output: ["pie"]
In this example, the regex /(?<!cherry )pie/
matches the word “pie” only if it is not preceded by the word “cherry” and a space. The (?<!cherry )
part is the negative lookbehind, which ensures that “cherry ” does not come before “pie” without including “cherry ” in the actual match.
Note that lookbehind assertions have limited browser support as of my knowledge cutoff in September 2021, and they are not supported in all JavaScript environments. Check the compatibility of lookbehind assertions before using them in a production environment.




Popular Category
Topics for You
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36
Go through our study material. Your Job is awaiting.