Skip to content Skip to sidebar Skip to footer

Common Words Detect In Different Fields Jquery

In jquery, how would you determine if one element uses a word that was being used in another element? For example: if one field contains the value: fishbucket, and another field co

Solution 1:

No need for jquery. Simple javascript will do. I can only give you a function to find longest in common starting string.

functioncommonStart(array){
    var x = array.slice(0).sort(), 
    word1 = x[0],
    word2 = x[x.length-1], 
    i= 0;
    while(word1.charAt(i) == word2.charAt(i)) ++i;
    return word1.substring(0, i);
}

e.g.

var testArray = ['testone','testtwo','testthree'];
    commonStart(testArray);

will yield 'test'

The standard tool for doing this sort of thing in Bioinformatics is the BLAST program. It is used to compare two fragments of molecules (like DNA or proteins) to find where they align with each other - basically where the two strings (sometimes multi GB in size) share common substrings.

Solution 2:

First List out all the values in your form.

var inputvalue = []; //array declaration

$('input[type=text]').each(function(){
inputvalue.push($(this).val());  //saves value in array
});

then loop through all the values to get you common string by pattern matching you need to define common string should of how many characters long.

Solution 3:

The problem could be broken down to finding the longest common subsequence.

Here's a javascript implementation for finding LCS beween 2 words. What you'll need to do is push all words in an array and then call LCS for each 2 strings you want to compare. (If return length is >1, there is a common text in between them).

There are a few string matching algorithms (based on hashes), you may want to google them too. Use only if requirement is huge, for usual cases, LCS implementation should be sufficient.

Post a Comment for "Common Words Detect In Different Fields Jquery"