Regular Expressions

  1. a = /reg exp/: creates a regular expression object and initializes it with the given regular expression.

  2. regular expressions are like perl regular expressions

    1. Example: /\d{4}/ finds any 4 digit number.
    2. Some features not mentioned when we covered php regular expressions
      1. modifiers
        Mode modifierDescriptionExample
        iCase insensitivity - Pattern matches upper or lowercase./aBc/i matches "abc" and "AbC".
        mMultiline - Pattern with ^ and $ match beginning and end of any line in a multiline string./^ab/m matches the second line of "cab\nabc", and /ab$/m matches the first line.
        gGlobal search - Pattern is matched repeatedly instead of just once./ab/g matches "ab" twice in "cababc".

      2. Minimal match: By default a regular expression matches the largest possible substring. However, this behavior is not always desirable. For example, suppose you want to extract the content between all h1 headers in a string. The natural regular expression to write is:
        /<h1>(.*)<\/h1>/g
        	     
        Unfortunately this regular expression will extract the substring starting at the first <h1> tag in the string and ending at the last </h1> tag in the string. For example, if the string is "<h1>Fast</h1> ... <h1>Slow</h1> ..." then the above regular expression will match "Fast</h1> ... <h1>Slow". What we want in this case is the minimal possible match and we can get that by putting a ? after the * symbol (this works with the + symbol as well):
        /<h1>(.*?)<\/h1>/g
        	     
        Now we will get the desired two matches of "Fast" and "Slow".

  3. methods for a regular expression

    1. test(string): returns true/false depending on whether or not the regular expression matches a substring in the string

    2. exec(string): returns an array with the substrings that match the parenthesized patterns in the regular expression

      • the first array entry contains the substring that matches the entire pattern
      • if the regular expression contains the g modifier, then you can iterate through the string using the regular expression. For example:
        let dates = "03-06-1948 08-02-2021 12-31-1999";
        let dateRegEx = /(\d{2})-(\d{2})-(\d{4})/g;	    
        let date = dateRegEx.exec(dates);
        while (date !== null) {
              console.log("Date: " + date[0] + " Year: " + date[3]);  
              date = dateRegEx.exec(dates);
        }
        	    

  4. methods for a string
    1. string.match(re): returns an array with the matched substring and any captured content.
    2. string.matchall(re): returns an iterable object that returns one array at a time with the matched substring and any captured content. For example:
      const matches = str.matchAll(/(\d+)/);
      
      for (const match of matches) { ... }
      	  
    3. string.replace(re, replacement_string): replace a matching substring with replacement_string

      1. put g at the end of the regular expression if you want all substrings replaced. For example--/\bBob\b/g.

      2. you can reshuffle text in a string, just as in perl:
              command: "Hi Brad Smiley".replace(/\b(Brad)\b\s+\b(Smiley)\b/, "$2 $1");
              result: "Hi Smiley Brad"
              

      3. string.search(re) returns the index of the first match between the regex and the given string, or -1 if no match is found.

      4. string.split(re): returns an array of strings created by separating the string into substrings based on a regex.