搜索脚本不会查找单个单词

时间:2013-04-14 09:40:57

标签: javascript search

我正在使用下面的脚本进行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 个答案:

答案 0 :(得分:1)

由于您正在使用正则表达式,您可以做的是用OR运算符|替换原始搜索字符串中的所有空格(&lt; - 它是一个管道[shift + \],以防它很难告诉)。您可以在keyword = keyword.replace(" ", "|");

之前添加第var exp = new RegExp(keyword,"gi");

请告诉我这是否适合您,如果有意义的话:)

一些参考文献:
JavaScript替换 - http://www.w3schools.com/jsref/jsref_replace.asp
RegExp运算符 - http://introcs.cs.princeton.edu/java/72regular/