【问题标题】:Get the String Count in a String - JavaScript [duplicate]获取字符串中的字符串计数 - JavaScript [重复]
【发布时间】:2021-03-25 17:58:42
【问题描述】:

如何在 JavaScript 中查找字符串中的字符串计数?

例如:

https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//

获取// 的出现次数。

【问题讨论】:

    标签: javascript birt


    【解决方案1】:

    var a="https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//"
    var count= a.split("//").length-1;
    console.log(count)

    你可以通过将其拆分为数组来找到计数并找到数组的长度

    var a="https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//"
    var count= a.split("//").length-1; // its 2
    

    【讨论】:

      【解决方案2】:

      var string = "https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//";
      console.log((string.match(/\/\//g) || []).length);

      你可以试试下面的代码:

      var string = "https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//";
      console.log((string.match(/\/\//g) || []).length);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        相关资源
        最近更新 更多