JSFiddle - React, Tailwind, and code Playground

HTML

<h2>
get the number of matches before cursor or before the location user is typing
</h2>

<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

CSS

#comment{
  
  width:400px;
  height:200px;
  
}

JavaScript

var p = 'josh';
var regEx = new RegExp(p,"g");


$("#comment").on("keyup",function() {

var number = $('#comment').val().match(regEx).length;

$("#write").html(number);

});