Skip to content

Instantly share code, notes, and snippets.

@AloofBuddha
Created May 2, 2023 19:03
Show Gist options
  • Save AloofBuddha/b71c52fb61e07b3b9257849883f529a3 to your computer and use it in GitHub Desktop.
Save AloofBuddha/b71c52fb61e07b3b9257849883f529a3 to your computer and use it in GitHub Desktop.

Sure, here's a programming assignment for teaching the basics of regular expressions in Javascript:

Programming Assignment: Regular Expressions in Javascript

In this assignment, you will be using regular expressions to validate and manipulate strings in Javascript.

Task 1: Validate Email Addresses

Create a function validateEmail(email) that takes an email address as input and returns true if the email address is valid and false otherwise. An email address is considered valid if it meets the following criteria:

  • It contains only one @ symbol
  • It contains at least one period (.) after the @ symbol
  • It does not contain any spaces
  • It has at least one character before and after the @ symbol

Task 2: Replace Phone Numbers

Create a function replacePhoneNumbers(text) that takes a string as input and replaces all instances of phone numbers with the text PHONE_NUMBER. A phone number is considered to be a ten-digit number that may or may not contain hyphens or parentheses, but must not contain any other characters. For example, (123) 456-7890 and 123-456-7890 are both valid phone numbers, but 1234567890a is not.

Task 3: Find URLs

Create a function findUrls(text) that takes a string as input and returns an array of all URLs in the string. A URL is considered to be any string that begins with http:// or https:// and ends with a space or a punctuation mark. For example, http://www.example.com and https://www.example.com?foo=bar are both valid URLs, but www.example.com is not.

Bonus Task: Password Validation

Create a function validatePassword(password) that takes a password as input and returns true if the password is valid and false otherwise. A password is considered valid if it meets the following criteria:

  • It contains at least 8 characters
  • It contains at least one uppercase letter, one lowercase letter, and one digit
  • It does not contain any spaces or special characters (except for _)

Submission Guidelines

Please submit a single Javascript file containing your implementation of the above tasks. Your code should be well-documented, easy to read, and include test cases for each function. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment