How to Find Vowels in a String in JavaScript


In this article, I will explain how to find Vowels in a String and how to find total number of vowels in a string in JavaScript. I have written simple script for checking vowels in a string in JavaScript. I have used regular expression. For me, a regular expression is best.

In this article, I will explain how to find Vowels in a String and how to find total number of vowels in a string in JavaScript.
I have written simple script for checking vowels in a string in JavaScript.
I have used regular expression. For me, a regular expression is best.

E.g.

How to Find total number of vowels in a string in JavaScript:



<html>
<head>
<script>
function vowels() {

var str=document.getElementById('employee_name').value;

var vowelC = 0;
var total_vowels="";
for (var i = 0; i < str.length; i++) {

if (str.charAt(i).match(/[a-zA-Z]/) != null) {

// vowels

if (str.charAt(i).match(/[aeiouAEIOU]/))
{

total_vowels=total_vowels+str.charAt(i);
vowelC++;

}



}
}
document.getElementById('vowels').value=total_vowels;
document.getElementById('vowels_count').value=vowelC;
alert("Total Number of vowels: " + vowelC);


}
</script>
</head>
<body>
<table border=1>
<tr>
<td>Enter Name :</td>
<td><input type='text' value='' id='employee_name' name='employee_name'></td>

</tr>
<tr>
<td>Vowels Count :</td>
<td><input type='text' readonly=true value='' id='vowels_count' name='vowels_count'></td>

</tr>


<tr>
<td>Vowels :</td>
<td><input type='text' readonly=true value='' id='vowels' name='vowels'></td>

</tr>
<tr>

<td colspan=2><input type='button' value='Click Me' onClick="javascript:vowels();"></td>
</tr>
</table>

</body>
</html>
<?php
//echo phpinfo();
?>


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: