user1825293
user1825293

Reputation: 167

JavaScript Strip Vowels

I am trying to strip vowels in a string. I know I should be using str.replace but I am baffled on how to get it into a span.

This is more of less what i am looking to do:

Write a JavaScript function that takes in a string s and returns the string which is equivalent to s but with all ASCII vowels removed. EX: (“Hello World”) returns: "Hll wrld"

Please help!

Upvotes: 15

Views: 49809

Answers (3)

pavelgj
pavelgj

Reputation: 2701

To replace vowels you can use a simple regular expression:

function removeVowels(str) {
  return str.replace(/[aeiou]/gi, '');
}

As for the part about getting it into a span, not 100% sure what you mean, but maybe something like this:

<span id="mySpan">Hello World!</span>
<script>
  var span = document.getElementById('mySpan');
  span.innerHTML = removeVowels(span.innerHTML);
</script>

Upvotes: 30

Srujan Kumar Gulla
Srujan Kumar Gulla

Reputation: 5851

string.replaceAll("[aeiou]\\B", "")

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

.replace(/[aeiou]/ig,'') is all you need.

Upvotes: 58

Related Questions