Jinah Lee

RegEx

10.10.2020

RegEx or regular expression is patterns used to match or locate character combinations in strings. It's commonly used to validate input value.

Use it to vaidate password

I used it in my last post to create Mailchimp registration example. Mailchimp requires the password to have

  • one lowercase character
  • one uppercase character
  • one number
  • one special character
  • 8 characters minimum

This can be validated with RegEx

if (!password.match(/(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[!@#$%^&*]).{8}/)) {
    return "error message"
}
  • (?=.[a-z]) matches any lowercase character
  • (?=.[A-Z]) matches any uppercase character
  • (?=.\d) matches any number
  • (?=.[!@#$%^&*]) matches any special character
  • .{8} at least 8 characters
Other uses

From the edabit javascript challenges

Question

From a given string comprised of uppercase and lowercase characters, detect lowercase characters, extract them and return it as one word.

detectWord("UcUNFYGaFYFYGtNUH")"cat"
Answer
const detectWord = str => {
  return str.replace(/[A-Z]/g, "")
}
  • /[A-Z]/g do a global search for uppercase characters
  • replace all uppercase characters with empty string " "
  • this leaves us with only lowercase characters
Question

How many D's are in a sentence?

countDs("My friend Dylan got distracted in school.")4
Answer
const countDs = str => {
  str.match(/d/gi).length
}
  • /d/id match character "d", case insensitive