Find matching word from ReGex

Find matching word from ReGex

by Shree Khanal

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

JavaScript

//flagComment();

//window.setInterval(function() {
  flagComment();
//}, 10 * 800);



async function flagComment() {
  const Time_Out = 3000;
  let getresult = [];
  let resultlength = 0;
  let keyword1 = ["love"];
  let keyword2 = ["thank(?:ed|s)?(?:\\s(you|it\\'s))?(?:\\s(very|so|for))?(?:\\s(much|help))?"];
  let keywords = $.merge(keyword1, keyword2);
  let regex = new RegExp(keywords.join("|"), 'gi');
  let response;
  response = ["thank you so much. saved my ass", "thank you so much. solved my problem"];
  console.log(regex);

  //getresult = response.map(a => a.body +" #@# "+ a.link +" #@# "+ a.comment_id + " #@# "+ a.post_id);
  let finalresult = $.grep(response, function(elem) {
    return (elem.length < 60 && elem.match(regex))
  });
  resultlength = finalresult.length;

  let filter = new RegExp(keywords.toString().replace(/,/g, '|'), 'gi');


  $.each(finalresult, function(ind, value) {
    setTimeout(function() {
      let comment = value;
      let matches = comment.match(regex) || [];
      let reason = '';

      if (matches.length) {
        for (var i = 0, l = matches.length; i < l; i++) {
          reason += ' ---' + matches[i] + '--- ';
        }
      }

      console.log("match-length:-" + matches.length + " match-reason:-" + reason + " value:-" + value);

    }, ind * Time_Out);

  });
}