我想指望用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