在文本中搜索特殊字符

时间:2013-08-04 09:59:00

标签: javascript search

我在下面有这个Javascript代码,用于搜索输入文本字段的任何单词。现在,需要搜索的文本包含特殊字符,例如示例文本中的撇号和点: “和Zeb'u·lun的部落。” < / p>

现在,我如何采用我的JS代码来包含这些特殊字符?如果我在文本字段中键入没有特殊字符的Zebulun,则搜索功能无法找到它。

   var SearchResultCount = 0;
    var a = new Array();
    var oneTime = false;

    // helper function, recursively searches in elements and their child nodes
    function HighlightAllOccurencesOfStringForElement(element,keyword) {
        if (element) {
            if (element.nodeType == 3) { // Text node
                while (true) {
                    var value = element.nodeValue; // Search for keyword in text node
                    var idx = value.toLowerCase().indexOf(keyword;

                    if (idx < 0) break; // not found, abort

                    var span = document.createElement("span");
                    var text = document.createTextNode(value.substr(idx,keyword.length));
                    span.appendChild(text);
                    span.setAttribute("class","MyAppHighlight");

                    text = document.createTextNode(value.substr(idx+keyword.length));
                    element.deleteData(idx, value.length - idx);
                    var next = element.nextSibling;
                    element.parentNode.insertBefore(span, next);
                    element.parentNode.insertBefore(text, next);
                    element = text;

                    span.scrollIntoView();
                    span.style.background= "-webkit-linear-gradient(top, #FAE309, #FFF7AA)"; 
                    span.style.fontWeight = "bold";
                    span.style.padding = "2px";
                    span.style.borderRadius = "5px";
                    span.style.boxShadow = "0px 0px 2px black";
                    a.push(span); // SET THIS CODE HERE
                    SearchResultCount++; // update the counter
                }

            } else if (element.nodeType == 1) { // Element node
                if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
                    for (var i=element.childNodes.length-1; i>=0; i--) {
                        HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
                    }
                }
            }
        }
    }

// the main entry point to start the search
function HighlightAllOccurencesOfString(keyword) {
    RemoveAllHighlights();
    HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}

1 个答案:

答案 0 :(得分:0)

首先,在以下行中缺少右括号:

var idx = value.toLowerCase().indexOf(keyword;

因此,如果该功能根本不起作用,我不会感到惊讶。

要回答您的问题,一种方法是使用String变量的本机replace()函数清除除字母字符之外的每个字符。您必须使用搜索字词来搜索您正在搜索的文字,因此您必须同时传递valuekeyword个变量功能。像这样:

keyword = cleanUp(keyword);
var value = cleanUp(element.nodeValue);
...
function cleanUp(toClean) {
    cleaned = toClean.replace([^a-zA-Z],""); //Deletes non-alphabetic characters (including spaces) by replacing them with nothing. If you want to leave spaces intact, use [^a-zA-Z ] instead.
    return cleaned;
}

完成后,使用您要比较两个字符串的相同功能。

相关问题