思路:使用正则表达式 代码 const str = "heLloWolrdeeeee"; const key = 'l'; (() => { const matchReg = new RegExp(`${key}`, 'ig');//正则目标:/l/ig const matchRes = str.match(matchReg) || [];//结果数组:[l,L,l]; console.log(matchRes.length);//3 })()