1

I am looking to search an XML document to match whatever word the user has entered. The problem I'm having is at best I can only get the number of the lines it occurs in, so if it occurs twice in one line it's only be counted as one instance.

I'm passing the search term in from an input box to this function:

function searchResults(query) {            
  var temp = "\\b" + query + "\\b";         
  var regex_query = new RegExp(temp, "gi");  
  var currentLine;
  var num_matching_lines = 0; 
  $("#mainOutput").empty();
  $("LINE", current_play_dom).each(function () {
    var line = this;
    currentLine = $(this).text();          
    matchesLine = currentLine.replace(regex_query, '<span class="query_match">' + query + '</span>');
    if (currentLine.search(regex_query) > 0) {
      $('#sideInfo>ul').empty();
      num_matching_lines++
      $("#mainOutput").append("<br /><p class='speaker_match'>"+  $(line).parent().find('SPEAKER').text() +"</p>");
      $("#mainOutput").append("<p class='act_match'>"+  $(line).parent().parent().parent().children(':first-child').text()+"</p>");
      $("#mainOutput").append("<p class='scene_match'>"+  $(line).parent().parent().children(':first-child').text() +"</p>");
      $("#mainOutput").append("<p>" + matchesLine + "</p>");
      $("#mainOutput").append("<br>");
    }   
  });
  $("#mainOutput").append("<p>" + matchesLine + "</p>");
  $("#sideInfo").append("<p>Found " + query + " " + num_matching_lines + " times</p>");
}

This is outputting the line count fine but I want to be able to output the amount of times the word actually occurs in the XML.

4

0 回答 0