Reputation: 429
What is the best way to match two strings that contain the same phrase? For example is there a good way to match the following two strings:
st1 = 'jenissplendidicecreams'
st2 = 'jenisicecream'
What would be the proper regex to match those two strings?
Upvotes: 0
Views: 69
Reputation: 4975
Lots of ways to do this.
You can just use string methods:
var name = st1.slice(0, st1.indexOf("splendidicecream"));
return name == st2.slice(0, st2.indexOf("icecream"));
Or if you really wanted to use regex:
var nameRe = /.+?(?=splendidicecream)/,
name = st1.match(nameRe)[0];
return st2.startsWith(name);
Or harder regex:
var nameRe = /.+?(?=splendidicecream)/,
startsWithNameRe = new RegExp("^" + st1.match(nameRe)[0]);
return startsWithNameRe.test(st2);
Upvotes: 0
Reputation: 3810
I am not quite sure but I think you might be looking for something like this?
var str1 = "I have some words!";
var str2 = "I have some very similar words!";
var min = 5;
var max = 6;
var len = str1.length;
for(var i = 0; i<len; i++)
{
for(var j = min; j<max; j++)
{
var re = new RegExp(str1.substring(i,j));
console.log(re);
//Do something when this returns true??
console.log(re.test(str2));
}
}
Upvotes: 1
Reputation: 14361
Problem is that computers have no idea where the words are. Either you could index a whole dictionary or use something that uses a specified distance to get values (if string
is str2 and str
is str1):
var distance = 8, // Best for your current case
end = new RegExp('.{'+distance+'}$', '').exec(string)[0],
start = new RegExp('^(.*)'+end+'$', '').exec(string)[1];
function matches (s) {
return new RegExp('^(?:'+start+').*(?:'+end+')$').test(s);
}
matches(str);
var min = 1, // Adjust depending on acceptance level
split = string.split('').reverse().map(function (a,i) {
if ( (i+1) % min === 0) {
return a + '.*';
} else {
return a;
}
}).reverse().join(''),
regex = new RegExp(split, '');
regex.test('jenissplendidicecreams');
Upvotes: 0
Reputation:
You need to build a regexp which looks like this:
/.*j.*e.*n.*i.*s.*i.*c.*e.*c.*r.*e.*a.*m.*/
This regexp matches if the string being tested includes all the original characters, in order, but with any arbitrary additional characters in between.
We can build that easily enough by doing
function make_regexp(str) {
var letters = str.split('');
letters.push(''), letters.unshift('');
return new RegExp(letters.join('.*'));
}
> make_regexp('jenisicecream')
< /.*j.*e.*n.*i.*s.*i.*c.*e.*c.*r.*e.*a.*m.*/
Now test if the second string matches:
> make_regexp('jenisicecream').test('jenissplendidicecreams')
< true
Upvotes: 2