简体   繁体   English

javascript /正则表达式中的字符串匹配

[英]string matching in javascript / regex

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? 匹配这两个字符串的正则表达式是什么?

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. 如果要测试的字符串按顺序包括所有原始字符,但中间包含任意其他附加字符,则此regexp匹配。

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

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)); } } 

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);

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) : 您可以为整个字典建立索引,也可以使用使用指定距离的东西来获取值(如果string是str2,而str是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);


Or you can have to computer guess: 或者,您可能需要计算机猜测:

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM