我刚开始学习JavaScript,并且卡在正则表达式中。这个程序应该计算输入字符串中元音的数量并显示元音。使用正则表达式计算字符串中的元音
如果元音没有放在一起,但如果元音一起出现,则不会给出正确的值,程序运行良好。 例如“树”。元音将显示为“EE”和计数为1。
Type a word here : <input type="text" id="word1">
<input type="button" value="Vowels" onclick="Vowels()">
function Vowels(){
var String1 = document.getElementById('word1').value;
var re = new RegExp(/[aeiou]+/gi);
var Vowels1 = String1.match(re);
alert(Vowels1);
if (Vowels1.length>=1)
{
alert("The number of vowels are:" + Vowels1.length);
}
else
{
alert("No vowels in the word");
}
}
请帮我找到合适的正则表达式,并else
说法是行不通的。请帮我修复它。
只需删除'+'。所以'“树”.match(/ [aeiou]/gi).length'给你'2' –