gpt4 book ai didi

使用 Swift 突出显示 Javascript 搜索文本

转载 作者:行者123 更新时间:2023-11-30 10:35:27 24 4
gpt4 key购买 nike

我正在为 iOS 应用程序制作一个文本搜索机制(如 ⌘ + F),它正在工作,但我有两个问题。

  1. 每当有人用阿拉伯语搜索某些内容时,该词就会断开连接。

  2. 用户无法搜索文本中是否有变音符号,但他们的搜索却没有(所以基本上我试图使其不区分变音符号)

这是我突出显示的代码(我从 this 找到):

var uiWebview_SearchResultCount = 0;

/*!
@method uiWebview_HighlightAllOccurencesOfStringForElement
@abstract // helper function, recursively searches in elements and their child nodes
@discussion // helper function, recursively searches in elements and their child nodes

element - HTML elements
keyword - string to search
*/

function uiWebview_HighlightAllOccurencesOfStringForElement(element,keyword) {
if (element) {
if (element.nodeType == 3) { // Text node

var count = 0;
var elementTmp = element;
while (true) {
var value = elementTmp.nodeValue; // Search for keyword in text node
var idx = value.toLowerCase().indexOf(keyword);

if (idx < 0) break;

count++;
elementTmp = document.createTextNode(value.substr(idx+keyword.length));
}

uiWebview_SearchResultCount += count;

var index = uiWebview_SearchResultCount;

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

//we create a SPAN element for every parts of matched keywords
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
var spacetxt = document.createTextNode("\u200D");//\u200D
span.appendChild(text);
span.appendChild(spacetxt);

span.setAttribute("class","uiWebviewHighlight");
span.style.backgroundColor="#007DC8a3";
span.style.borderRadius="3px";

index--;
span.setAttribute("id", "SEARCH WORD"+(index));
//span.setAttribute("id", "SEARCH WORD"+uiWebview_SearchResultCount);

//element.parentNode.setAttribute("id", "SEARCH WORD"+uiWebview_SearchResultCount);

//uiWebview_SearchResultCount++; // update the counter

text = document.createTextNode(value.substr(idx+keyword.length));
element.deleteData(idx, value.length - idx);

var next = element.nextSibling;
//alert(element.parentNode);
element.parentNode.insertBefore(span, next);
element.parentNode.insertBefore(text, next);
element = text;
}


} 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--) {
uiWebview_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
}
}
}
}
}

// the main entry point to start the search
function uiWebview_HighlightAllOccurencesOfString(keyword) {
uiWebview_RemoveAllHighlights();
uiWebview_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}

// helper function, recursively removes the highlights in elements and their childs
function uiWebview_RemoveAllHighlightsForElement(element) {
if (element) {
if (element.nodeType == 1) {
if (element.getAttribute("class") == "uiWebviewHighlight") {
var text = element.removeChild(element.firstChild);
element.parentNode.insertBefore(text,element);
element.parentNode.removeChild(element);
return true;
} else {
var normalize = false;
for (var i=element.childNodes.length-1; i>=0; i--) {
if (uiWebview_RemoveAllHighlightsForElement(element.childNodes[i])) {
normalize = true;
}
}
if (normalize) {
element.normalize();
}
}
}
}
return false;
}

// the main entry point to remove the highlights
function uiWebview_RemoveAllHighlights() {
uiWebview_SearchResultCount = 0;
uiWebview_RemoveAllHighlightsForElement(document.body);
}

function uiWebview_ScrollTo(idx) {
var idkNum = uiWebview_SearchResultCount - idx
var scrollTo = document.getElementById("SEARCH WORD" + idkNum);
if (scrollTo) scrollTo.scrollIntoView();
}

我还发现了this这实际上正是我想要的(不会断开单词并且对变音符号不敏感),但它是在 JQuery 中,我不知道如何在我的代码中实现它。

最佳答案

您可以将字符串转换为 NSString,然后使用 range(of:options:),而不是使用 indexOf。 :

var range = value.range(of: keyword, options: [.caseInsensitive, .diacriticInsensitive])

关于使用 Swift 突出显示 Javascript 搜索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58152675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com