如何避免循环遍历数组来查找部分匹配?
我正在循环遍历英语短语数组,如果我找到与当前文本节点的匹配项,我会将其替换为 non_english 数组中的翻译。所有这些都可以 100% 准确匹配。
但对于部分匹配,我需要使用 .match 命令,它允许部分匹配。
我搜索完全匹配的代码如下:
var 找到 = $.inArray(value,en_lang);
然后,如果找到值,则替换文本。这种方法很快,我喜欢它。
但是,要进行部分单词/短语匹配,我必须使用此循环代码。
// loop thru language arrays
for (var x = en_count; x > 0; x--) {
// assign current from/to variables for replace
var from = en_lang[x];
var to = other_lang[x];
// if value match do translation
if (value.match(from)) {
content(node, value.replace(from, to));
}
// mark this node as translated
if ($.browser.msie == 'false') {
$(node).data('translated', 'yes');
}
这
可以完成工作,但速度相当慢。经过大量研究,我发现我可以通过 join 命令将英语数组转换为基于列表的字符串。
但我无法想出一个函数来搜索此列表中的部分匹配项,并返回列表中的位置。
我正在尝试这个 2006 年创建的旧 js 函数。但我不知道如何正确地恢复位置。
function listfind(list, value, delimiters) {
if (!delimiters) {
var delimiters = ','
}
_TempListSplitArray = list.split(delimiters)
var FoundIdx = 0;
for (i = 0; i < _TempListSplitArray.length; i++) {
if (_TempListSplitArray[i] == value) {
FoundIdx = i + 1;
break
}
if (value.match(_TempListSplitArray[i])) {
FoundIdx = i + 1;
break
}
}
return FoundIdx
}
感谢您抽出时间。
I am looping through an array of english phrases, and if i find a match, with the current text node, i replace it with it's translation in the non_english array. All of that works 100% for exact matches.
But for partial matches, I need to use the .match command, which allows for partial matches.
My code to search for exact matches is like this:
var found = $.inArray(value,en_lang);
Then if there is a found value, then do replacement of text. This method is fast and I love it.
However to do partial word/phrase matching, I have to use this looping code.
// loop thru language arrays
for (var x = en_count; x > 0; x--) {
// assign current from/to variables for replace
var from = en_lang[x];
var to = other_lang[x];
// if value match do translation
if (value.match(from)) {
content(node, value.replace(from, to));
}
// mark this node as translated
if ($.browser.msie == 'false') {
$(node).data('translated', 'yes');
}
}
This does the job but is pretty slow. After a lot of research, I have found that I can convert the english array to a list-based string via the join command.
But I am unable to come up with a function to search this list for a partial match, and return the position in the list.
I was trying out this old js function created in 2006. But I can't figure out how to get the position back, correctly.
function listfind(list, value, delimiters) {
if (!delimiters) {
var delimiters = ','
}
_TempListSplitArray = list.split(delimiters)
var FoundIdx = 0;
for (i = 0; i < _TempListSplitArray.length; i++) {
if (_TempListSplitArray[i] == value) {
FoundIdx = i + 1;
break
}
if (value.match(_TempListSplitArray[i])) {
FoundIdx = i + 1;
break
}
}
return FoundIdx
}
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Javascript 有一个 foreach 类型的系统,但它仍然基于循环
这是循环数组的最佳选择,但这种方式也适用于对象
更新了对您问题的评论
您只需替换您知道的文字即可
Javascript has a foreach type of system but its still based on a loop
Thats the best your getting for looping though an array but this way allso works with objects
UPDATED for Comments on your question
You can just replace the text you know