Regular Expression - Letter Edition

| 3 min read

😬 What is Regular Expression? 😬

Archived blogs from here

What are regular expressions? They are patterns that are used to match characters in provided strings.

When I first looked into this, it looks so scary! Since I do codewars daily, I encountered a lot of solutions that are solved with this. I almost pulled my hair out! However, each problem tends to focus on one type of regular expression.

In general, regular expressions tend to wrap with 2 forward slashes.

πŸ‘‰ /pattern/g πŸ‘ˆ

g is one of the expression flags and stands for global. Regular expressions only search for a single match by default. g enables to search for more than 1 match.

e.g.

  • If you want to find all upper cases, the pattern is /[A-Z]/g
  • If you want to find all lower cases, the pattern is /[a-z]/g
  • If you want to find both lower and upper cases, the pattern is /\w/g
  • If you want to find all non-letters, the pattern is /\W/g

When you only focus on one type at a time, it is not so overwhelming. So I am slowly learning about it little by little over time. In the end, it is not bad after all. And it can be really powerful and useful once you get it.

Codewars problems:

First problem

Write a function that takes a single string (word) as an argument. The function must return an ordered list containing the indexes of all capital letters in the string.

Example:

Test.assertSimilar( capitals(β€˜CodEWaRs’), [0,3,4,6] );

πŸ‘£ Steps:

  • set regex for capital letters
  • use indexOf() to find capital letters’ index
const capitals = (word) => {
	const regex = /[A-Z]/g;
	const capIndex = (char) => word.indexOf(char);
	return word.match(regex).map(capIndex);
};

Second problem

Return the number (count) of vowels in the given string. We will consider a, e, i, o, u as vowels for this Kata (but not y). The input string will only consist of lowercase letters and/or spaces.

πŸ‘£ Steps:

  • set up aeiou regex
  • check for match
  • if true, return length, otherwise, return 0
function getCount(str) {
	const vowels = /[aeiou]/gi;
	const checkMatch = str.match(vowels);
	return checkMatch !== null ? checkMatch.length : 0;
}

i is also one of the expression flags, i stands for ignore, which means it will ignore the case of the letter.

Example: /aBc/i would match AbC.

Resources

MDN - Regular expressions

RegExr: Learn, Build, & Test RegEx

regex101: build, test, and debug regex

Thank you!

Thank you for your time and reading. I hope this helps you. Please feel free to let me know if you have any questions or feedback. πŸ˜€