0

I have the following code which works fine for english but not working for arabic. how can i use this code for arabic also. how can I serach it as a string

    function highlightSearch() {
    var text = document.getElementById("query").value;
    var query = new RegExp("(\\b" + text + "\\b)", "gim");
    var e = document.getElementById("nav-5-3-primary-ver").innerHTML;
    var enew = e.replace(/(<h6>|<\/h6>)/igm, "");
    document.getElementById("nav-5-3-primary-ver").innerHTML = enew;
    var newe = enew.replace(query, "<h6>$1</h6>");
    document.getElementById("nav-5-3-primary-ver").innerHTML = newe;
    }          
    #nav-5-3-primary-ver h6{
    background-color:#FF9;
    color:#555;
}
     <input name="query" id="query" type="text" size="30" maxlength="30">
     <input name="searchit" type="button" value="Search" onClick="highlightSearch()">

      <div id="nav-5-3-primary-ver"> 
       hello i am this
      </br>
      اذا كان الجهاز خارج التغطية هل يتم تسجيل البيانات للسيارة؟
      </div>
salman m
  • 33
  • 7

1 Answers1

0

There's a gotcha with \b (in javascript at least) : the notion of "word" it uses (to detect word boundaries) is :

  • sequences of characters in the set [a-zA-Z0-9_] (latin alphanumerics, plus the _ char)

It won't work as you expect for other charsets (note : even latin diacritics - letters with accents or signs on them - don't work).


Choose some other way to chek that you matched a complete word :

  • as suggested in this answer : you can write a predicate that matches "a space character or ^ or $"

  • or match the word without boundaries, and for each match, inspect the characters prior and next to that match


Links to ECMAscript specifications :

LeGEC
  • 46,477
  • 5
  • 57
  • 104