DEV Community

Randy Rivera
Randy Rivera

Posted on

Notes(3)

Match Beginning String Patterns

  • Prior posts showed that regular expressions can be used to look for a number of matches.
  • They are also used to search for patterns in specific positions in strings.
  • In an earlier challenge, you used 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.
  • Ex: Let's use the caret character in a regex to find Alan only in the beginning of the string randyAndAlan.
let randyAndAlan = "Alan and Randy both like racing.";
let calRegex = /^Alan/;
let result = calRegex.test(randyAndAlan);

console.log(result); will display true
Enter fullscreen mode Exit fullscreen mode

Match Ending String Patterns

  • In the above post, you learned to use the caret character to search for patterns at the beginning of strings. There is also a way to search for patterns at the end of strings.
  • You can search the end of strings using the dollar sign character $ at the end of the regex.
  • Ex:
let randyAndAlan = "Alan and Randy both like racing.";
let calRegex = /^Alan/;
let result = calRegex.test(randyAndAlan);
let noEnding = "Randy and Alan are racing across the world!"
let error = lastRegex.test(noEnding);

console.log(result); will display true
console.log(error); will display false
Enter fullscreen mode Exit fullscreen mode

Match All Letters and Numbers

  • Using character classes, you were able to search for all letters of the alphabet with [a-z]. This kind of character class is common enough that there is a shortcut for it, although it includes a few extra characters as well. 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 (_).
  • Ex: Here we usedthe 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; 
let result = quoteSample.match(alphabetRegexV2).length;

console.log(result); will display 31
Enter fullscreen mode Exit fullscreen mode

Match Everything But Letters and Numbers

  • You've learned that you can use a shortcut to match alphanumerics [A-Za-z0-9_] using \w. A natural pattern you might want to search for is the opposite of alphanumerics.
  • You can search for the opposite of the \w with \W. Note, the opposite pattern uses a capital letter. This shortcut is the same as [^A-Za-z0-9_].
  • Ex:
let quoteSample = "The five boxing wizards jump quickly.";
let nonAlphabetRegex = /\W/g; 
let result = quoteSample.match(nonAlphabetRegex).length;

console.log(result); will display 6
Enter fullscreen mode Exit fullscreen mode

If you really wanted to see what it shows simply take out .length

let quoteSample = "The five boxing wizards jump quickly.";
let nonAlphabetRegex = /\W/g; 
let result = quoteSample.match(nonAlphabetRegex);

console.log(result); will display
[ ' ', ' ', ' ', ' ', ' ', '.' ] 
Enter fullscreen mode Exit fullscreen mode
  • Note: spaces count

Match All Numbers

  • You've learned shortcuts for common string patterns like alphanumerics. Another common pattern is looking for just digits or numbers.
  • The shortcut to look for digit characters is \d, with a lowercase d. This is equal to the character class [0-9], which looks for a single character of any number between zero and nine.
  • Ex: Here we 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

console.log(result); will display 4
Enter fullscreen mode Exit fullscreen mode

Match All Non-Numbers

  • The shortcut to look for non-digit characters is \D. This is equal to the character class [^0-9], which looks for a single character that is not a number between zero and nine.
  • Ex: Here we 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;
let result = movieName.match(noNumRegex).length;

console.log(result); will display 17
Enter fullscreen mode Exit fullscreen mode

Top comments (0)