简体   繁体   中英

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.

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.

Condition:

  1. The match should be case sensitive 'Dog' and 'dog' are not the same thing
  2. The match should consider spaces 'dog' and 'dog ' are not the same thing.
 <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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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