Reputation: 5412
I have two strings of equal number of slashes and the same letter in each position. There is always one letter and a square bracket indicating an index. There will always be a corresponding letter in both strings.
var first = "/a/b[1]/c/d[3]/e[1]/f"
var second = "/a/b[1]/c/d[4]/e/f"
I expect output should be
result = "/a/b[1]/c/d/e/f"
This is what I came up with but maybe there's a better way of doing it as it returns /a/b/c/d/e/f
which is not what i wanted. http://jsfiddle.net/3PM9H/
var first = "/a/b[1]/c/d[3]/e[1]/f".split("/");
var second = "/a/b[1]/c/d[4]/e/f".split("/");
for (var i=0; i < first.length; i++){
firstMatch = first[i].match(/\[([0-9]+)\]/g);
secondMatch = second[i].match(/\[([0-9]+)\]/g);
if (firstMatch != secondMatch){
first[i] = first[i].replace(/\[([0-9]+)\]/g,'') //get rid of the square bracket.
}
}
first.join("/");
Upvotes: 0
Views: 37
Reputation: 60788
I just solved this here. It's a node project but the main file is dependency-less hence portable.
Use the dynamic programming algorithm to compute the Levenshtein distance, and use that transform to generate the regex. Subsitutes become wildcards, inserts or deletes become optional characters.
var Levenshtein = require('levenshtein-transformation)';
var lev = new Levenshtein(str1, str2);
console.log(lev.regex());
Upvotes: 1