DEV Community

Cover image for JavaScript Using RegEx
Rutik Bhoyar
Rutik Bhoyar

Posted on

4 3

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

Neon image

Set up a Neon project in seconds and connect from a Next.js application

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

Top comments (0)

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

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay