如何检查是否包含任何字符串顺序的所有关键字? RegExp Javascript

时间:2017-01-19 17:27:02

标签: javascript regex posix

我想检查字符串是否包含字符串的任何顺序的所有输入关键字。 在很多情况下,关键字按任何顺序排列,但存在于字符串中。

示例(这是我期望的):

// Same order and have all keywords
"Hello world!".contains( "hello world" )       // true

// Same order and have all keywords
"Hello all in the world!".contains( "hello world" )       // true

// Any order but have all keywords
"Hello world!".contains( "world hello" )       // true

// Same order and all keywords
"Hello world!".contains( "worl hell" )         // true

// Have all keywords in any order
"Hello world!".contains( "world" )             // true

// No contains all keywords
"Hello world!".contains( "where you go" )      // false

// No contains all keywords
"Hello world!".contains( "z" )                 // false

// No contains all keywords
"Hello world!".contains( "z1 z2 z3" )          // false

// Contains all keywords in any order
"Hello world!".contains( "wo" )                // true

我尝试:

/(?=\bhello\b)(?=\bworld\b)/i.test("hello world")      // false
/(?=.*?hello.*?)(?=.*?world.*?)/i.test("hello world")  // false
/^(?=\bhello\b)(?=\bworld\b).*?$/i.test("hello world") // false

我创建了一些函数,如:

// escape string to use in regexp
String.prototype.escape = function () {
    return this.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
};
// check if empty string
String.prototype.isEmpty = function () {
    return this.length === 0;
};
// check if contain keywords...
String.prototype.contains = function (keywords) {
    var value  = '^(?=\\b' + keywords
        .escape()
        .replace(/(^\s+|\s+$)/ig, '')
        .replace(/\s+/, ' ')
        .split(/\s+/)
        .join('.*?)(?=.*?') + ').*$',
    reg = new RegExp(value, 'i'),
    text = this;
    return reg.test( this );
};

由于

6 个答案:

答案 0 :(得分:4)

您的意思是无序的关键字列表:Regex - match multiple unordered words in a string,但删除每个正则表达式令牌的最后\b部分

/(?=.*?\bhello)(?=.*?\bworld).*/i

使用这些类型的正则表达式,所有测试都应该通过。

http://regexr.com/3f3ru

中查看

i使其忽略区分大小写,以防您需要同时检查 Hello wOrld

答案 1 :(得分:2)

I.G. Pascual的答案显示了如何构建解决OP问题的正则表达式。下面是工作代码,演示如何动态构建此类正则表达式,从OP传递所有测试用例。



function buildRegEx(str, keywords){
  return new RegExp("(?=.*?\\b" + 
    keywords
      .split(" ")
      .join(")(?=.*?\\b") +                     
    ").*", 
    "i"
  );
}

function test(str, keywords, expected){
  var result = buildRegEx(str, keywords).test(str) === expected
  console.log(result ? "Passed" : "Failed");
}

// Same order and have all keywords 
test("Hello world!", "hello world", true);
// Same order and have all keywords 
test("Hello all in the world!", "hello world", true);
// Any order but have all keywords 
test("Hello world!", "world hello", true);
// Same order and all keywords 
test("Hello world!", "worl hell", true);
// Have all keywords in any order 
test("Hello world!", "world", true);
// No contains all keywords 
test("Hello world!", "where you go", false);
// No contains all keywords
test("Hello world!", "z", false);
// No contains all keywords 
test("Hello world!", "z1 z2 z3", false);
// Contains all keywords in any order 
test("Hello world!", "wo", true);




答案 2 :(得分:1)

基于前瞻的选项的替代方案是:

String.prototype.contains = function (keywordsStr) {
    var keywords = keywordsStr.split(/\s+/);
    return keywords.every(function(keyword)) {
        var reg = new RegExp(keyword.escape());
        return reg.test(this);
    }, this);
};

答案 3 :(得分:1)

String.prototype.contains = function(string){    
    var keywords = string.split(" ");
    var contain = true;

    for(var i = 0; i < keywords.length && contain; i++){
        if(keywords[i] == "") continue;

        var regex = new RegExp(keywords[i], "i");
        contain = contain && regex.test(this);
    }

    return contain;
}

答案 4 :(得分:0)

将.match()方法用于字符串是一种简单的方法。 你可以试试这个

示例:

var re = /(hello|world)/i;
var str = "Hello world!"; 
console.log('Do we found something?', Boolean(str.match(re)));

// for other
var re = /(hello|world)/i;       // true
var re = /(world|hello)/i;       // true
var re = /(worl|hell)/i;         // true
var re = /(this|is|my|world)/i;  // true
var re = /(where|you|go)/i;      // false
var re = /(z)/i;                 // false
var re = /(z1|z2|z3)/i;          // false
var re = /(wo)/i;                // true

答案 5 :(得分:0)

result.filter(camp => search.map(keyword => camp.name.includes(keyword))。reduce((acc,curr)=> acc && curr,true));