UIWebView JavaScript Highlight - 如何高亮特殊字符? (utf8编码?)

UIWebView JavaScript Highlight - how to Highlight special characters? (utf8 encoding?)

我一直在努力解决这个问题,但我有点放弃了搜索。我不太了解 Javascript 但我的 UIWebView 有一个 .js 高亮功能。

我的问题是它不会突出显示包含任何特殊字符的文本,例如:“',à 等”

我正在将 NSString 解析到 .js 函数中,这可能会导致问题,但我不知道是否必须解析 utf8 字符,或者是否必须将字符串转换为 .js 中的 utf8 .

这是我的 .js 代码

function MyApp_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");
          span.style.backgroundColor="#C4B695";
          span.style.color="black";
          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();
          MyApp_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--) {
                    MyApp_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
            }
          }
      }
   }
}

// the main entry point to start the search
function MyApp_HighlightAllOccurencesOfString(keyword) {
  // MyApp_RemoveAllHighlights();
  MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}

请让我知道我可以提供哪些其他信息。

万一其他人对此感到疑惑,解决方案是一个简单的转义字符。谢谢大家。

editedSearchString = [editedSearchString stringByReplacingOccurrencesOfString:@"á" withString:@"\á"];