Part 2



Course Link

1. Find Characters with Lazy Matching

  • A greedy match finds the longest possible part of a string that fits the regex pattern and returns it as a match.

    • Defaulft for Regular Expression
  • A lazy match finds the smallest possible part of the string that satisfies the regex pattern.

    • Use ?
  • Example 1

// string "titanic"
// A greedy match
/t[a-z]*i/ //return titani

// A lazy match
/t[a-z]*?i/ //return ti
// 
// return the HTML tag <h1>
let text = "<h1>Winter is coming</h1>";
let myRegex = /<.*?>/; // Change this line
let result = text.match(myRegex);

2. Match Beginning String Patterns

  • Use the caret character (^) inside a character set to create a negated character set in the form [^thingsThatWillNotBeMatched].
  • Outside of a character set, the caret is used to search for patterns at the beginning of strings.
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^Cal/; // Change this line
let result = calRegex.test(rickyAndCal);

3. Match Ending String Patterns

  • Use $ at the end of the regex
let theEnding = "This is a never ending story";
let storyRegex = /story$/;
storyRegex.test(theEnding);
// Returns true
let noEnding = "Sometimes a story will have to end";
storyRegex.test(noEnding);
// Returns false

4. Match All Letters and Numbers

  • Use shortcut character classes (shorthand character classes)
  • A shortcut of \[A-Za-z0-9_]\ is \w
  • Example 1
let longHand = /[A-Za-z0-9_]+/;
let shortHand = /\w+/;
let numbers = "42";
let varNames = "important_var";
longHand.test(numbers); // Returns true
shortHand.test(numbers); // Returns true
longHand.test(varNames); // Returns true
shortHand.test(varNames); // Returns true
  • Example 2
// Use the shorthand character class \w to count the number of alphanumeric characters in various quotes and strings 
let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /\w/g; // Change this line
let result = quoteSample.match(alphabetRegexV2).length;

5. Match Everything But Letters and Numbers

  • A shortcut of [^A-Za-z0-9_] is \W
  • Use the shorthand character class \W to count the number of non-alphanumeric characters in various quotes and strings
  • Example 1
let shortHand = /\W/;
let numbers = "42%";
let sentence = "Coding!";
numbers.match(shortHand); // Returns ["%"]
sentence.match(shortHand); // Returns ["!"]
  • Example 2
// Use the shorthand character class \W to count the number of non-alphanumeric characters in various quotes and strings
let quoteSample = "The five boxing wizards jump quickly.";
let nonAlphabetRegex = /\W/g; 
let result = quoteSample.match(nonAlphabetRegex).length;

6. Match All Numbers

  • A shortcut of character class [0-9] is \d, which looks for a single character of any number between zero and nine
// Use the shorthand character class \d to count how many digits are in movie titles. Written out numbers ("six" instead of 6) do not count.
let movieName = "2001: A Space Odyssey";
let numRegex = /\d/g; 
let result = movieName.match(numRegex).length;

7. Match All Non-Numbers

  • A shortcut for non-digits [^0-9] is \D
//Use the shorthand character class for non-digits \D to count how many non-digits are in movie titles
let movieName = "2001: A Space Odyssey";
let noNumRegex = /\D/g; // Change this line
let result = movieName.match(noNumRegex).length;

8. Restrict Possible Usernames 📓

You need to check all the usernames in a database. Here are some simple rules that users have to follow when creating their username.

    1. Usernames can only use alpha-numeric characters.
    1. The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
    1. Username letters can be lowercase and uppercase.
    1. Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
let username = "JackOfAllTrades";
let userCheck = /^[a-z]([0-9][0-9]+|[a-z]+\d*)$/i;
 // Change this line
let result = userCheck.test(username);

9. Match Whitespaces or Spaces between Letters

  • You can search for whitespace using \s.
  • This pattern not only matches whitespace, but also carriage return, tab, form feed, and new line characters. You can think of it as similar to the character class [\r\t\f\n\v].
  • Example 1
let whiteSpace = "Whitespace. Whitespace everywhere!"
let spaceRegex = /\s/g;
whiteSpace.match(spaceRegex);
// Returns [" ", " "]
  • Example 2
//Change the regex countWhiteSpace to look for multiple whitespace characters in a string.

10. Match Non-Whitespace Characters

  • Search for non-whitespace using \S
  • This pattern will not match whitespace, carriage return, tab, form feed, and new line characters. You can think of it being similar to the character class [^ \r\t\f\n\v]
let whiteSpace = "Whitespace. Whitespace everywhere!"
let nonSpaceRegex = /\S/g;
whiteSpace.match(nonSpaceRegex).length; // Returns 32