简体   繁体   English

在文本中搜索特殊字符

[英]Search for Special Characters in Text

I have this Javascript code below that searches for any word entered into a textfield. 我下面有这个Javascript代码,用于搜索输入到文本字段中的任何单词。 Now, the text that needs to be searched through contains special characters like the apostrophe and dot in this sample text: "And the tribe of Zeb′u·lun." 现在,需要搜索的文本在此示例文本中包含特殊字符,例如撇号和点: “还有Zeb'u·lun部落”。

Now, how can I adopt my JS code to include those special characters? 现在,如何采用我的JS代码来包含那些特殊字符? If I type Zebulun with no special characters in my textfield the search function cannot find it. 如果我在文本字段中键入不带特殊字符的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());
}

First off, there's a closing parenthesis missing in the following line: 首先,以下行中缺少右括号:

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

So I wouldn't be surprised if the function didn't work at all. 因此,如果该功能根本不起作用,我不会感到惊讶。

To answer your question, one way to do this is to wash out every character except alphabetic characters using the String variable's native replace() function. 要回答您的问题,一种方法是使用String变量的本机replace()函数清除除字母字符之外的所有字符。 You'd have to do this with both the search term and the text you're searching, so you'll have to pass both your value and your keyword variables through the function. 您必须同时使用搜索词要搜索的文本,因此您必须同时通过函数传递valuekeyword变量。 Something like this: 像这样:

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

Once this is done, use the same function you've got going to compare the two strings. 完成此操作后,请使用将要比较两个字符串的相同函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM