Search code examples
javascriptregexstring-matchingcharsequence

Matching only characters in sequence of a word from a given string


I am trying to find a closest match for a word by giving a specific string, for example:

so I would have:

"jonston" x "john"  => "jo" //only "jo" is the part that matches
"joshua" x "john" => "jo" 
"mark" x "marta"    => "mar"

as you can see I only would like to retrieve the characters in sequence matching, that's why joshua and john only would have jo in common sequence and not joh since both have the letter h

I've tried that with regular expression by using the following:

"john".match(/["joshua"]+/) //=> outputs ["joh"] and not ["jo"]

is there any way I could match only the first chars that match?

I will be using javascript for the implementation

I hope that makes sense

Thanks in advance


Solution

  • initLCS = function(a, b) {
        for (var i = 0; i < a.length && a[i] == b[i]; i++);
        return a.substr(0, i);
    }
    
    
    initLCS("jonston", "john") // jo
    initLCS("jonston", "j111") // j
    initLCS("xx", "yy") // ""
    

    If you insist on using regular expressions, it goes like this:

    initLCS = function(a, b) {
    
        function makeRe(x) {
            return x.length ? "(" + x.shift() + makeRe(x) + ")?" : "";
        }
    
        var re = new RegExp('^' + makeRe(b.split("")), "g");
        return a.match(re)[0];
    }
    

    This creates an expression like /^(j(o(h(n)?)?)?)?/g from the second string and applies it to the first one. Not that it makes much sense, just for the heck of it.