Javascript正则表达式匹配即将出现

时间:2017-10-12 07:11:19

标签: javascript html regex null match

我正在尝试编写一个接收字符串的javascript函数,并计算元音的数量。显示每个元音的数量,以及总数。如果每个元音都在字符串中,它可以正常工作,但是如果没有A或E,它将返回null。

有没有办法可以拦截这个并将null替换为0?或者有更有效的方法来实现这一目标吗?感谢任何可以提供帮助的人!

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>

3 个答案:

答案 0 :(得分: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 :(得分: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>

答案 2 :(得分:-1)

length的结果为match时,您可以添加使用条件运算符来输出0而不是null

为了使其更具可读性,您可以预先获取长度,然后在最终字符串中使用它。

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;