Join Regular Classroom : Visit ClassroomTech

JavaScript – codewindow.in

Related Topics

HTML

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

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:
  1. We define the wordToReplace and replacement variables to store the word to be replaced and the replacement word, respectively.
  2. We construct a regular expression regex using the RegExp constructor with the g flag to make sure it matches all occurrences of the word in the text.
  3. We use the replace() method on the text string, passing in the regex and the replacement as arguments. The method replaces all occurrences of the word “fox” with “cat”.
  4. 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"
  1. 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"
  1. 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"
  1. String methods with regular expressions: Besides match(), you can use other string methods like search(), split(), replace(), and exec() 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.
  1. 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.
  1. 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 = /(?&lt;=apple )pie/g;
const matches = text.match(regex);

console.log(matches); // Output: [&quot;pie&quot;]
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.
  1. 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 = /(?&lt;!cherry )pie/g;
const matches = text.match(regex);

console.log(matches); // Output: [&quot;pie&quot;]
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

HTML

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

Go through our study material. Your Job is awaiting.

Recent Posts
Categories