【发布时间】:2021-03-25 17:58:42
【问题描述】:
如何在 JavaScript 中查找字符串中的字符串计数?
例如:
https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//
获取// 的出现次数。
【问题讨论】:
标签: javascript birt
如何在 JavaScript 中查找字符串中的字符串计数?
例如:
https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//
获取// 的出现次数。
【问题讨论】:
标签: javascript birt
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
【讨论】:
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);
【讨论】: