JavaScript Using RegEx

rutikab12

Rutik Bhoyar

Posted on December 11, 2021

JavaScript Using RegEx

Problem:

Return the number (count) of vowels in the given string.
We will consider a, e, i, o, u as vowels.

The input can consist of Lower case and upper case letters so make sure to count both of them

function getCount(str) {
    return str.match(/[aeiou]/gi).length
}
Enter fullscreen mode Exit fullscreen mode

This will give you the count of vowels in javascript.

Stop gninnipS My sdroW!

Problem:
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.

function spinWords(string) {

 return string.replace(/\w{5,}/g, 
    function(w) {return w.split('').reverse().join('')}
  )
}
Enter fullscreen mode Exit fullscreen mode

This will be give correct output like below.

spinWords(“Hey fellow warriors”) //returns “Hey wollef sroirraw"
spinWords("This is a test") //returns "This is a test"
spinWords("This is another test") //returns "This is rehtona test"
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
rutikab12
Rutik Bhoyar

Posted on December 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Beginners JavaScript
javascript Beginners JavaScript

August 22, 2024

Closures in JavaScirpt
javascript Closures in JavaScirpt

May 19, 2022