2

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.

crosenblum
  • 1,869
  • 5
  • 34
  • 57
  • `:contains` in jquery handles partial search http://api.jquery.com/contains-selector/ the only thing that i can think is that it might not work so well with non elements, so u can have a think about this , http://stackoverflow.com/questions/1789945/javascript-string-contains hope it helps – Val Feb 23 '11 at 15:50
  • But that's for a selector, I already have the elements I need, now I need to search for partial matches inside the current text node. – crosenblum Feb 23 '11 at 15:53
  • Is there a reason you can not just skip the splitting/looping over the array all together and just do a find replace with a regular expression on the large string? – epascarello Feb 23 '11 at 16:01
  • What constitutes a "partial match"? Can you provide a short example with concrete values? – Sir Robert Feb 23 '11 at 16:04
  • That's the purpose of this question, to find a way to not loop. What kind of regular expressions? Especially since i will be applying them to a variable that holds the current node text. – crosenblum Feb 23 '11 at 16:05
  • Sure let us say, I have a text node that says "Welcome:" but I need to search for just the word "Welcome" and replace that with it's translation, because of how $.inarray it will only find exact matches, and to do partial matches, i need to use the .match command and loop thru the array. I am hoping someone has an idea other than looping thru the array. since that is too big a performance hit. Does that help? – crosenblum Feb 23 '11 at 16:07
  • Would you need to match on partial word matches? For example, if you needed to replace "road", would you want to match "roadblock" or are you only considering full words? – Jeff Feb 23 '11 at 16:42
  • i have an existing function to take care of exact matches, but that misses some words/phrases because of spacing or special characters, so I need a "fast" partial search thru an array for matches, to catch the rest. Make sense? – crosenblum Feb 23 '11 at 16:58

1 Answers1

0

Javascript has a foreach type of system but its still based on a loop

var array = ['hello', 'world'];
for(var key in array){
  alert(array[key]);
}

Thats the best your getting for looping though an array but this way allso works with objects

var obj = {'one':'hello', 'two':'world'];
for(var key in obj){
   alert("key: "+key+" value: "+obj[key]);
}

UPDATED for Comments on your question You can just replace the text you know

var str = "hello World";
str = str.replace("hello", "Bye bye");
alert(str);
Barkermn01
  • 6,781
  • 33
  • 83
  • But my goal is to avoid loops, as they especially in this case is performance heavy. – crosenblum Feb 23 '11 at 16:05
  • sorry then the only other way is to know your order of your array and use the access without needing to go though it. – Barkermn01 Feb 23 '11 at 16:08
  • I'm saying if you dont have to go though a all the values then dont store the key of where you need to access and use that otherwise your stuck to a memory eating loop sorry JavaScript just does not have the power or PHP – Barkermn01 Feb 23 '11 at 16:33
  • Store the key? What do you mean by that? Understand the logic flow 1st. 1. loop thru all text nodes, 2. find exact match for current text of node in array, 3. Which works by looping thru array and then using value.match looks for partial matches. But the problem is that the looping method takes too long. And $.inArray only works on exact matches. Any other ideas? – crosenblum Feb 23 '11 at 17:00
  • all i can say is check this out http://phpjs.org/ it might give you the functions you require – Barkermn01 Feb 24 '11 at 09:31