Skip to content Skip to sidebar Skip to footer

Extract Matched Text And Its Neighboring Context N Words Upstream N Words Downstream (nth Occurrence From Left And Nth Occurrence From Right)

I need text that fits nicely in an autocomplete dropdown. This required more strategy than it would initially appear. Using hidden text or fadeout option didn't work well because t

Solution 1:

Regex can simplify this a lot! Here is my solution:

var haystack ="L5 L4 L3 L2 L1 needle R1 R2 R3 R4 R5 R6",
    needle ="needle",
    numWords =3;

var result = haystack.match("(?:\\s?(?:[\\w]+)\\s?){"+numWords+"}"+needle+"(?:\\s?(?:[\\w]+)\\s?){"+numWords+"}");

console.log("With "+ numWords +" neighboring words: \""+ result[0] +"\"");

With 3 neighboring words: " L3 L2 L1 needle R1 R2 R3 "

Solution 2:

varhaystack="L5 L4 L3 L2 L1 needle R1 R2 R3 R4 R5 R6";
varneedle="needle";
vardelim=" ";
varnumWords=3;
vartrimmedText= trimToNeighboringText(haystack, needle, numWords, delim);

console.log("With " + numWords + " neighboring words: \"" + trimmedText + "\"");

function trimToNeighboringText(haystack, needle, numWords, delim) {

    // number of delimiter occurrences to look for,// this assumes the same on both sidesvarnumDelims= numWords + 1;

    // this splits on the text that is matchedvartokens= haystack.split(needle);

    if (tokens.length > 1) {

        varleftEllipsis="";
        varrightEllipsis="";

        // Get the index of the start character within the left neighbor,// working backwardsvarstartIndex= nthOccurrenceBackwards(tokens[0], delim, numDelims);
        //console.log(startIndex + ": " + tokens[0].substr(startIndex));// if text is truncated at leftif (startIndex > 0) {
            leftEllipsis = "... ";
        }
        // if text is not truncated at leftelsestartIndex=0;

        // Get the index of the end character within the right neighbor// working forwards (note that start is local to right neighbor)varendIndex= nthOccurrenceForwards(tokens[1], delim, numDelims);

        // if text is truncated at rightif (endIndex > 0) {
            rightEllipsis = " ...";
        }
        // if text is not truncated at rightelse {
            endIndex = tokens[1].length;
        }

        // Concatenate the left fragment, the needle, and the right fragmentreturn (leftEllipsis + tokens[0].substr(startIndex) + needle
                             + tokens[1].substr(0, endIndex) + rightEllipsis);

    } else {
        console.warn("Match not found");
        return haystack;
    }
}

function nthOccurrenceForwards(str, pat, n) {
    if (str.length == 0) return0;
    //console.log("\""+str+"\"");vari= -1;
    while (n-- && i++ < str.length) {
        i = str.indexOf(pat, i);
        if (i==-1) break;
        //console.log("n and i "+n + "," + i)
    }
    return i;
}

function nthOccurrenceBackwards(str, pat, n) {
    if (str.length == 0) return0;
    varj= str.length;
    while (n-- && j-- > 1) {
        j = str.lastIndexOf(pat, j);
    }
    return j;
}

Post a Comment for "Extract Matched Text And Its Neighboring Context N Words Upstream N Words Downstream (nth Occurrence From Left And Nth Occurrence From Right)"