1

I have a Textarea box, a textbox and a button. I would like on clicking the button, for the word in the textbox to be checked against words in the textarea and count number of occurrence.

I have tried last 2 days to write a click function to do this but not getting anywhere since not sure what codes or logic follows next. Only managed to read the contents in the textarea but not sure how to get the word in the textbox and search against sentence in textarea.

Please I am a newbie in JQuery so not asking for anyone to write the code but more of a guide if possible. If this question isn't permitted here, I am more than happy to delete it. Thanks

Chelseawillrecover
  • 2,596
  • 1
  • 31
  • 51
  • 5
    `I have tried last 2 days to write a click function to do this but not getting anywhere since not sure what codes or logic follows next.` - show this code. Even if it's minimal, at least we have a start and reference to what you are working with. Even a jsFiddle would be great – Ian Mar 12 '14 at 22:09
  • I will create a fiddle and update the question – Chelseawillrecover Mar 12 '14 at 22:18
  • Depending on where you are, a few things to look into are: [on()](https://api.jquery.com/on/) for event handling, [val()](http://api.jquery.com/val/) for getting input values, and [regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) – Ian Mar 12 '14 at 22:19
  • 1
    After regular expressions as @Ian said, take a look at the [match](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) method it could help you in your quest – Eduardo Quintana Mar 12 '14 at 22:37
  • thanks @EduardoQuintana, your comment really helped and points me in the right direction for further reading – Chelseawillrecover Mar 12 '14 at 23:15

3 Answers3

1

Use string.match() along with processing to ensure the first string is not empty and that there actually is a match. Did the following in jQuery since you seemed interested in using it.

var textVal = $('#textbox').val();
var textArea = $('#textarea').val();
var matching = new RegExp('\\b' + textVal + '\\b','g');             
var count = textArea.match(matching);
var result = 0;

if (count !== null) {
    result = count.length;
}

http://jsfiddle.net/promiseofcake/t8Lg9/3/

Lucas
  • 51
  • 3
0

You are looking for string occurrences, so take a look at this thread.

You could do this using match(), as suggested in the comments:

var m = searchText.match(new RegExp(wordMatch.toString().replace(/(?=[.\\+*?[^\]$(){}\|])/g, "\\"), "ig"));
// m.length contains number of matches

But that will also match partial words, like 'fox' in 'foxy'. So another method is to split the input into words and walk over them one by one:

var count = 0;
var words = searchText.split(' ');
for (x in words) {
    if (words[x].toLowerCase() == wordMatch) {
        count++;
    }
}

Take a look at this full example: http://jsfiddle.net/z7vzb/

Community
  • 1
  • 1
tschoffelen
  • 518
  • 7
  • 21
0
<input type="text"/>
<textarea>...</textarea>
<button>Get</button>
<div></div>
<script>
$("button").click(function(){
count=$("textarea").text().split($("input[type=text]").val()).length-1;
$("div").html(count);
})
</script>
user3410311
  • 582
  • 4
  • 6