• Use + to look for one or more characters
  • Use * to look for zero or more characters.
  • You can search the end of strings using the $ character 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
    
  • Start of String or Line ^ By default, the ^ anchor specifies that the following pattern must begin at the first character position of the string.
  • The closest character class in JavaScript to match the alphabet is \w.

This shortcut is equal to [A-Za-z0-9_]. This character class matches upper and lowercase letters plus numbers. Note, this character class also includes the underscore character ( _ ).

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

Use the shorthand character class \w to count the number of alphanumeric characters in various quotes and strings.

These shortcut character classes are also known as shorthand character classes.

  • You can search for the opposite of the \w (non-alphanumerics) with \W. This shortcut is the same as [^A-Za-z0-9_].

  • The shortcut to look for digit characters is \d. This is equal to the character class [0-9], which looks for a single character of any number between zero and nine.

  • Non-digit characters is \D. This is equal to the character class [^0-9].

  • 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].

  • For non-whitespace matching use \S. You can think of it being similar to the character class [^ \r\t\f\n\v].

  • To specify a certain range of patterns, use {} .

    let ohStr = "Ohhh no";
    let ohRegex = /Oh{3,6}\s.*/; // match only 3 to 6 letter h's in the word "Oh no"
    let result = ohRegex.test(ohStr);
    

To only specify the lower number of patterns, keep the first number followed by a comma, like /a{3,}/.

  • Use the caret ^ character inside a character set to create a negated character set in the form [^thingsThatWillNotBeMatched].
  • Outside of a character set, the ^ is used to search for patterns at the beginning of strings. These are called quantity specifiers. You put two numbers between the curly brackets - for the lower and upper number of patterns.

For example, to match only the letter a appearing between 3 and 5 times in the string "ah", your regex would be /a{3,5}h/.

Change the regex haRegex to match the word “Hazzah” only when it has four or more letter z’s.

let haStr = "Hazzzzah";
let haRegex = /Haz{4,}.*/; // Change this line
let result = haRegex.test(haStr);
  • If you want only a specific number of pattern matches, just have that one number between the curly brackets. For example, to match only the word “hah” with the letter a 3 times, your regex would be /ha{3}h/

  • Sometimes the patterns you want to search for may have parts of it that may or may not exist. However, it may be important to check for them nonetheless.

You can specify the possible existence of an element with a ?. This checks for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.

let american = "color";
let british = "colour";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american); // Returns true
rainbowRegex.test(british); // Returns true
  • Lookaheads are patterns that tell JavaScript to look-ahead in your string to check for patterns further along. This can be useful when you want to search for multiple patterns over the same string.

There are two kinds of lookaheads: positive lookahead and negative lookahead.

A positive lookahead will look to make sure the element in the search pattern is there, but won’t actually match it. A positive lookahead is used as (?=...) where the ... is the required part that is not matched.

On the other hand, a negative lookahead will look to make sure the element in the search pattern is not there. A negative lookahead is used as (?!...) where the ... is the pattern that you do not want to be there. The rest of the pattern is returned if the negative lookahead part is not present.

Lookaheads are a bit confusing but some examples will help.

let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex); // Returns ["q"]
noquit.match(qRegex); // Returns ["q"]

A more practical use of lookaheads is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:

let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true
  • To search for repeat substrings using capture groups ( ) You put the regex of the pattern that will repeat in between the parentheses.

To specify where that repeat string will appear, you use a backslash \ and then a number. This number starts at 1 and increases with each additional capture group you use. An example would be \1 to match the first group.

The example below matches any word that occurs twice separated by a space:

let repeatStr = "regex regex";
let repeatRegex = /(\w+)\s\1/;
repeatRegex.test(repeatStr); // Returns true
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]

Using the .match() method on a string will return an array with the string it matches, along with its capture group.

  • You can search and replace text in a string using .replace() on a string. The inputs for .replace() is first the regex pattern you want to search for. The second parameter is the string to replace the match or a function to do something.
let wrongText = "The sky is silver.";
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue");
// Returns "The sky is blue."

You can also access capture groups in the replacement string with dollar signs ($).

"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"
  • Typical processing of strings is to remove the whitespace at the start and end of it.

Note The .trim() method would work here, but you’ll need to complete this challenge using regular expressions.

let hello = "   Hello, World!  ";
let wsRegex = /^(\s*)|(\s*$)/g; // Change this line
let result = hello.replace(wsRegex, ''); // Change this line
  • https://regex101.com/