【问题标题】:Search for Special Characters in Text搜索文本中的特殊字符
【发布时间】:2013-08-04 09:58:59
【问题描述】:

我在下面有这个 Javascript 代码,用于搜索输入到文本字段中的任何单词。现在,需要搜索的文本包含此示例文本中的撇号和圆点等特殊字符:"And the tribe of Zeb′u·lun."

现在,我怎样才能采用我的 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());
}

【问题讨论】:

    标签: javascript search


    【解决方案1】:

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

    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;
    }
    

    完成后,使用与比较两个字符串相同的函数。

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多