2013-04-14 108 views
1

我正在使用下面的腳本進行我的HTML網站搜索。搜索腳本不尋找單個詞

我遇到的問題是,當我希望它在整個XML中獨立搜索單詞時,它一起搜索單詞作爲一個短語。即如果有人輸入「數字製作者」,則它分別搜索「數字製作者」,而不是單詞「數字」和「製作者」。

任何幫助得到這個工作將不勝感激!

$(document).ready(function(){ 

//GLOBAL VAR 
var XMLSource = $('#data').attr('xmlData'); 
var keyword = ''; 
var catType = ''; 
var pub = ''; 

var i = 0; 

$("#searchButton").click(function() { 
    keyword = $("input#term").val(); 
    catType = $("#category option:selected").val(); 

    //Reset any message 
    var errMsg = ''; 
    pub = ''; 

    if (keyword == '') { errMsg += 'Please Enter Search Term' + '\n'; } 
    else if (catType == 'none') { errMsg += 'Please Select Category' + '\n'; } 
    else { searchThis(); } 

    if (errMsg != '') { 
     pub += '<div class="error">' + '\n'; 
     pub += errMsg; 
     pub += '</div>' + '\n'; 
    } 

    //Show error 
    $('#result').html(pub); 

}); 

function searchThis() {    
    $.ajax({ 
     type: "GET", 
     url: XMLSource, 
     dataType: "xml", 
     success: function(xml) { loadPublication (xml) }  
    }); 
} 

function loadPublication (xmlData) { 
    i = 0; 
    var row; 

    var searchExp = ""; 

    $(xmlData).find('Job').each(function(){ 

     var SearchTitle = $(this).find('SearchTitle').text(); 
     var Description = $(this).find('Description').text(); 
     var EmailTo = $(this).find('EmailTo').text(); 

     //Format the keyword expression 
     var exp = new RegExp(keyword,"gi"); 

     //Check if there is a category selected; 
     //if not, use EmailTo column as a default search 
     if (catType == 'SearchTitle') { searchExp = SearchTitle.match(exp); } 
     else if (catType == 'Description') { searchExp = Description.match(exp); } 
     else if (catType == 'EmailTo') { searchExp = EmailTo.match(exp); } 

     if (searchExp != null) { 

      //Start building the result 
      if ((i % 2) == 0) { row = 'even'; } 
      else { row = 'odd'; } 

      i++;     

      pub += '<tr class="row ' + row + '">' + '\n'; 
      pub += '<td valign="top" class="col1">' + SearchTitle + '</td>' + '\n'; 
      pub += '<td valign="top" class="col2">' + Description + '</td>' + '\n'; 
      pub += '<td valign="top" class="col3">' + EmailTo + '</td>' + '\n'; 
      pub += '</tr>' + '\n'; 
     } 
    }); 

    if (i == 0) { 
     pub += '<div class="error">' + '\n'; 
     pub += 'No Result Found' + '\n';  
     pub += '</div>' + '\n'; 

     //Populate the result 
     $('#result').html(pub); 
    } 
    else { 
     //Pass the result set 
     showResult (pub); 
    } 
} 

function showResult (resultSet) { 

    //Show the result 
    pub = '<table id="grid" border="0">' + '\n'; 
    pub += '<thead><tr>' + '\n'; 
    pub += '<th class="col1">Job Title</th>' + '\n'; 
    pub += '<th class="col2">Job Description</th>' + '\n'; 
    pub += '<th class="col3">Apply To</th>' + '\n'; 
    pub += '</tr></thead>' + '\n'; 
    pub += '<tbody>' + '\n'; 

    pub += resultSet; 

    pub += '</tbody>' + '\n'; 
    pub += '</table>' + '\n'; 

    //Populate 
    $('#result').html(pub) 

    $('#grid').tablesorter(); 
} 
}); 

回答

1

由於您使用正則表達式,你所能做的就是用OR運算符|(<更換所有的空間在原始搜索字符串 - 這是一個管[SHIFT + \]的情況下,就很難告訴)。爲此,您可以添加行權keyword = keyword.replace(" ", "|");var exp = new RegExp(keyword,"gi");

讓我知道這對你的作品,如果它是有道理的:)

一些參考:
JavaScript的替換 - http://www.w3schools.com/jsref/jsref_replace.asp
正則表達式運算符 - http://introcs.cs.princeton.edu/java/72regular/