DEV Community

Cover image for JavaScript Using RegEx
Rutik Bhoyar
Rutik Bhoyar

Posted on

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

Oldest comments (0)

Some comments have been hidden by the post's author - find out more