0

I am trying to get a result of how many matches a specific word comes back in a textfield on keyup on an input field. So far I've managed to log a success message if the input matches a word in the textfield. Does someone know how I can check how many times this word is counted in the textfield?

This is what i got so far

var keyWordNL = $('input[name*="keyword-nl_NL"]');

keyWordNL.on('keyup', function(){
var keyWordNL = $('input[name*="keyword-nl_NL"]').val();
var editor2 =  $('#introduction-nl_NL').val();
  if(editor2.match(keyWordNL)) {
   console.log('success');
  } else {
   console.log('fail');
  }
});
Larsmanson
  • 413
  • 1
  • 5
  • 18
  • What is it you're struggling with? If it is the string matching, take a look at this question ["_How to count string occurence in string_"](http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string) – Christopher Moore Mar 14 '17 at 11:26
  • Because in that example, they are matching a string with a string (temp.match(/is/g). Where "is" is a string. I am matching a string with an input on keyup. Tried this example but couldn't get it working – Larsmanson Mar 14 '17 at 11:40

1 Answers1

0
var keyWordNL = $('input[name*="keyword-nl_NL"]').val(); //keyword to search
var editor2 =  $('#introduction-nl_NL').val(); // searched will be performed in this field
regex = new RegExp(keyWordNL,"g");
console.log((editor2.match(regex) || []).length);
Dan Ionescu
  • 3,135
  • 1
  • 12
  • 17