简体   繁体   English

计算文本区域中特定单词的出现次数 (JavaScript)

[英]Count Occurrence of Specific Word in the Textarea (JavaScript)

I want to write a simple JavaScript that Counts the number of times a word ( or a set of characters) that are typed into input field appear in the textarea field.我想编写一个简单的 JavaScript 来计算输入字段中输入的单词(或一组字符)出现在textarea字段中的次数

Example: Imagine someone pastes a long paragraph into textarea, and wants to see how many times does the word "dog" or phrase "dog on the leash" appears in the text.示例:假设有人将一个长段落粘贴到 textarea 中,并想查看“dog”一词或短语“dog on the leash”在文本中出现了多少次。

Condition:健康)状况:

  1. The match should be case sensitive 'Dog' and 'dog' are not the same thing匹配应该区分大小写'Dog' 和 'dog' 不是一回事
  2. The match should consider spaces 'dog' and 'dog ' are not the same thing.匹配应该考虑空格'dog' 和 'dog' 不是一回事。
 <textarea id="txtarea" cols="30" rows="5"></textarea> <input type="text" id="input" cols="15" rows="1"></input>

Could someone please shed some light有人可以说明一下吗

 var temp = "This is a dog, Dog."; console.log((temp.match(/dog/) || []).length);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM