I need to get the number of matches in a textarea, but this number is before user is typing.
For example:
@josh @marie @josh @anne <user is typing here> @josh @marie
suppose I want to get @josh, I need to have 2 in my variable, because I have two josh
before the cursor/before user is typing.
Any ideas how to get it?
My try:
var p = 'josh';
var regEx = new RegExp(p,"g");
$("#comment").on("keyup",function() {
var number = $('#comment').val().match(regEx).length;
$("#write").html(number);
});
but I can only get the total matches, not only the matches before cursor/before user is typing.
html:
<textarea id=comment>@josh @marie @anne @josh @anne @marie @chloe @josh @anne @josh @marie @anne @josh @anne @marie @chloe @josh @anne</textarea>
<br>
<span id=write></span> matches