Sure, here's a programming assignment for teaching the basics of regular expressions in Javascript:
In this assignment, you will be using regular expressions to validate and manipulate strings in Javascript.
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
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.
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.
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
_
)
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!