JSFiddle - React, Tailwind, and code Playground

by S.M. Murad Hasan Tanvir

HTML

<textarea id=comment>josh marie anne josh anne marie chloe josh anne josh marie anne josh anne marie chloe josh anne</textarea>

CSS

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

JavaScript

var p = 'josh';
var username = 'JJJ';

var out = replaceMatch( $("#comment").val(), p, username, 2)

$("#comment").val(out);

function replaceMatch(originalString, searchFor , replaceWith, matchNumber)
{
  var match = 0;
  return originalString.replace(searchFor, function(found){
    match++;
    return (match===matchNumber)?replaceWith:found;
  },'g');
}