2017-10-12 65 views
2

我正在嘗試編寫一個接收字符串的javascript函數,並計算元音的數量。顯示每個元音的數量,以及總數。如果每個元音都在字符串中,它會正常工作,但如果例如沒有A或E,它將返回null。Javascript正則表達式比賽即將推出null

有沒有辦法我可以截取這個並用null替換null?還是有更高效的方法來實現這一目標?感謝任何能夠幫助的人!

function countVowels(inString) { 
 
    return outString = (
 
    "Total vowels: " + inString.match(/[aeiou]/gi).length + 
 
    "\nTotal A's: " + inString.match(/[a]/gi).length + 
 
    "\nTotal E's: " + inString.match(/[e]/gi).length + 
 
    "\nTotal I's: " + inString.match(/[i]/gi).length + 
 
    "\nTotal O's: " + inString.match(/[o]/gi).length + 
 
    "\nTotal U's: " + inString.match(/[u]/gi).length 
 
); 
 
}
<form> 
 
    Enter a string to count its vowels. <br> 
 
    <input type="text" id="inString"><br> 
 
    <button type="button" onclick="console.log(countVowels(inString.value))">Count vowels</button> 
 
</form>

+0

呀,'match'是一種惱人的方式。我會嘗試一種不同的方法:編寫一個'函數計數(字符串,字母)'來計算'string'中'letter'的實例數,並用'count(inString,'a')',' count(inString,'e')'等等。你可以保存每一個並將它們加在一起得到元音的總數。 – Ryan

+0

而不是'/ [a]/gi',你可以使用'/ a/gi'。這不會解決你的問題,但它有點清潔...... – Cerbrus

回答

4

可以的情況下,使用|| []爲默認的 「返回值」 .match返回null

function countVowels(inString) { 
 
    return outString = (
 
    "Total vowels: " + (inString.match(/[aeiou]/gi) || []).length + 
 
    "\nTotal A's: " + (inString.match(/a/gi) || []).length + 
 
    "\nTotal E's: " + (inString.match(/e/gi) || []).length + 
 
    "\nTotal I's: " + (inString.match(/i/gi) || []).length + 
 
    "\nTotal O's: " + (inString.match(/o/gi) || []).length + 
 
    "\nTotal U's: " + (inString.match(/u/gi) || []).length 
 
); 
 
}
<form> 
 
    Enter a string to count its vowels. <br> 
 
    <input type="text" id="inString"><br> 
 
    <button type="button" onclick="console.log(countVowels(inString.value))">Count vowels</button> 
 
</form>

另外,請注意我的所有單字符刪除[]火柴。在正則表達式中,[a]a是等效的。

如果該端是"truthy",則||將返回運算符的左側。
如果左側是"falsy"||將始終返回語句的右側,這是我們的默認值。

如果.match找到任何結果,它將返回一個數組,它是「truthy」。
如果.match未找到任何結果,則返回null,這是「虛假」。

1

問題不是正則表達式中而是在邏輯。

對於給定的inString測試,它沒有元音ao。所以正則表達式不會找到任何匹配,這會失敗。

你可以嘗試這樣的事情:

原始代碼:

function countVowels(inString) { 
 
    return outString = (
 
    "Total vowels: " + (inString.match(/[aeiou]/gi) || []).length + 
 
    "\nTotal A's: " + (inString.match(/[a]/gi) || []).length + 
 
    "\nTotal E's: " + (inString.match(/[e]/gi) || []).length + 
 
    "\nTotal I's: " + (inString.match(/[i]/gi) || []).length + 
 
    "\nTotal O's: " + (inString.match(/[o]/gi) || []).length + 
 
    "\nTotal U's: " + (inString.match(/[u]/gi) || []).length 
 
); 
 
}
<form> 
 
    Enter a string to count its vowels. <br> 
 
    <input type="text" id="inString"><br> 
 
    <button type="button" onclick="console.log(countVowels(inString.value))">Count vowels</button> 
 
</form>

更新的代碼

function countVowels(inString) { 
 
    var vowels = "aeiou"; 
 
    var ret = "Total vowels: " + getMatchLength(inString, vowels); 
 
    for(var i = 0; i< vowels.length; i++) 
 
    ret += "\nTotal " + vowels[i].toUpperCase() + "'s: " + getMatchLength(inString, vowels[i]) 
 
    return ret; 
 
} 
 

 
function getMatchLength(str, chars) { 
 
    return (str.match(new RegExp("["+ chars + "]")) || []).length; 
 
}
<form> 
 
    Enter a string to count its vowels. <br> 
 
    <input type="text" id="inString"><br> 
 
    <button type="button" onclick="console.log(countVowels(inString.value))">Count vowels</button> 
 
</form>

-1

match的結果爲null時,您可以添加使用條件運算符來輸出0而不是length

爲了使其更具可讀性,您可以事先獲取長度,然後在最終的字符串中使用它。

function countVowels(inString) { 
 
    var aCount = inString.match(/[a]/gi) !== null ? inString.match(/[a]/gi).length : 0; 
 
    
 
    var eCount = inString.match(/[e]/gi) !== null ? inString.match(/[a]/gi).length : 0; 
 
    
 
    var iCount = inString.match(/[i]/gi) !== null ? inString.match(/[a]/gi).length : 0; 
 
    
 
    var oCount = inString.match(/[o]/gi) !== null ? inString.match(/[a]/gi).length : 0; 
 
    
 
    var uCount = inString.match(/[u]/gi) !== null ? inString.match(/[a]/gi).length : 0; 
 
    
 
    var vowelsCount = aCount + eCount + iCount + oCount + uCount; 
 
    
 
    var outString = "Total vowels: " + vowelsCount + 
 
    "\nTotal A's: " + aCount + 
 
    "\nTotal E's: " + eCount + 
 
    "\nTotal I's: " + iCount + 
 
    "\nTotal O's: " + oCount + 
 
    "\nTotal U's: " + uCount; 
 
    
 
    return outString; 
 
}
<form> 
 
Enter a string to count its vowels. <br> 
 
<input type="text" id="inString"><br> 
 
<button type="button" onclick="console.log(countVowels(inString.value))">Count vowels</button> 
 
</form>

+2

這是一種浪費,因爲你執行每個正則表達式匹配兩次。 –

+0

@TimothyGroote是的,你是對的。剛剛檢查下Cerbrus的答案。感謝您指出。 –

相關問題