0

我正在使用 highlight-text-inside-a-textarea 插件https://codersblock.com/blog/highlight-text-inside-a-textarea/工作正常,但我对重音词有疑问。例如,这是我要突出显示的关键字数组,$kw = array("excel","Réseau","R","electrom");但正如您所见,关键字“electromécanique”正在突出显示一半。我怎样才能避免这种情况?这可能与正则表达式有关。

在此处输入图像描述

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="/jquery/highlight-within-textarea.js"></script>
<link rel="stylesheet" type="text/css" href="/jquery/highlight-within-textarea.css"/>

</style>
</head>
<body>

<h1>Highlighted Keywords Test</h1>

<div id="job">
  <textarea cols="50" rows="5">excel Réseau Robert electromécanique R electromecanique</textarea></div>

<?php

$kw = array("excel","Réseau","R","electrom");

        foreach($kw as $kws) {
   
      $keywords_highlight[] = $kws;
      
        }

?>

<script>    

var arrHighlight =  <?php echo json_encode($keywords_highlight); ?>;

function escapeRegExp(arrHighlight) {
  return arrHighlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

var pattern = new RegExp("(?<![\\w-])(?:" + arrHighlight.map(escapeRegExp).join("|") + ")(?![\\w-])", "gi");

$('textarea').highlightWithinTextarea({
    highlight: pattern
});

</script>

</body>
</html>
4

0 回答 0