我想指望用match
方法更长的文本中的每个词的出现,但不是result
我只得到一个错误计数的单词的出现在使用JavaScript字符串:使用匹配
Cannot read property 'length' of null
我功能如下:
const myText = "cat dog stop rain cat"
myText.split(" ").forEach((word) => {
const numberOfOccurrences = myText.match(/word/g).length
console.log(`${word} - ${numberOfOccurrences}`)
})
我怎样才能修复它得到亲每个结果?
你是什么意思*你的表达式返回一个数组,它有一个条目,因此它总是返回1 *? – Li357