代码之家  ›  专栏  ›  技术社区  ›  shrimppunk

Javascript正则表达式匹配出现null

  •  2
  • shrimppunk  · 技术社区  · 6 年前

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

    有什么方法可以拦截它并用0替换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>
    3 回复  |  直到 6 年前
        1
  •  7
  •   Cerbrus    6 年前

    您可以使用 || [] 作为默认的“返回值” .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" 这个 || 将始终返回语句的右侧,这是我们的默认值。

    如果 .火柴
    如果 .火柴 没有找到任何结果,返回 无效的 ,即“falsy”。

        2
  •  1
  •   Rajesh Rahul    6 年前

    问题不在正则表达式中,而是在逻辑中。

    对于给定的 inString 测验 ,它没有元音 a o

    您可以尝试以下方式:

    原始代码:

    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;
    }
    <表单(>);
    输入字符串以计算其元音&书信电报;br>
    <button type=“button”onclick=“控制台日志(countvoxels(inString.value))”>计算元音(</按钮(>);
        3
  •  -1
  •   Mohit Bhardwaj    6 年前

    可以将use条件运算符添加到输出0,而不是 length match null .

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

    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>