How to Remove All Vowels From a String in JavaScript

How to Remove All Vowels From a String in JavaScript

Introduction

In this tutorial we will create a How to Remove All Vowels From a String in JavaScript. This tutorial purpose is to teach you how to dynamically remove a vowel in a string. This will cover all the basic parts of the function that will remove vowel from a string. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program purpose is to show how you can remove vowels from a certain string. I will give my best to provide you the easiest way of creating this program Remove vowel from string. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the string and the buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Remove All Vowels From a String</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16.  
  17. <a class="btn btn-default" href="index.html">Reset</a>
  18. <button class="btn btn-default" onclick="displayStr();">Remove All Vowels</button>
  19. <br /><br />
  20. <div id="result" align="justify" style="padding:10px; border:5px dashed #000; font-size:20px;">A snake charmer is a person who moves the streets with different types of the banks of the river Yamuna. It is snakes in his basket. He goes from one place to another to show various types of snakes and their tricks. He carries a pipe with which he plays music and snakes dance to his tune. He usually wears a colourful dress. The job of a snake charmer is quite dangerous. Some snakes are quite poisonous and can even bite him. It is not an easy task to catch and train them for the shows.</div>
  21. </div>
  22. <script src="script.js"></script>
  23. </body>
  24. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically remove all the vowels when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function displayStr(){
  2. let data = document.getElementById('result');
  3. data.innerHTML = removeVowels(data.innerHTML);
  4. }
  5.  
  6. function removeVowels(str){
  7. return str.replace(/[aeiou]/gi, '');
  8. }

The code above is the code that we use to remove the vowels from a string. In order to remove the vowels first we must create a method called removeVowels(). In this code it will only return the string value you have set, and it will replace all the vowels in the string using replace() function with a set regex. Lastly we will only create a method that will display the replaced vowel into the html content.

Output:

The How to Remove All Vowels From a String in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Remove All Vowels From a String in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment