0

I will be using coffeescript/javascript/jquery and I need to return true if a string contains all of my substrings.

So, lets say that I have an array of substrings 'my', 'dog', 'spot'. I need to return a true if my string is 'growing up my pet dog was named spot'. But I would need to return false if the string was 'I had a dog named spot'.

I am thinking there is a way to do this with a regular expression, but can't seem to figure it out?

4

5 回答 5

6
var mystring = "growing up my pet dog was named spot";
var words = ['my', 'dog', 'spot'];

var res = words.every(function(word) {
    return mystring.indexOf(word) !== -1;
});

如果您想在应用程序的各个部分重用该函数,请为其命名,并提供要搜索的字符串作为第二个参数,使其成为this值。

function hasWordInString(word) {
    return this.indexOf(word) !== -1;
}

var res = words.every(hasWordInString, mystring);

另一个好处.every()是它会短路 iffalse返回,因此一旦找到不匹配的单词,搜索就会停止,从而提高效率。


强制全词匹配

例如,如果要确保匹配的字符串是一个完整的单词以避免 "dog" 匹配 "dogma",则需要使用 RegExp 单词边界:

function hasWordInString(word) {
    return this.match(new RegExp("\\b"+word+"\\b"))!=null;
}
var res = words.every(hasWordInString,mystring);
于 2013-08-09T13:18:46.737 回答
1

书籍的另一种方法:

var str1 = "growing up my pet dog was named spot";
var str2 = "I had a dog named spot";
var words = ['my','dog','spot'];
function str_match_array(str,array)
{
    var found = array.filter(function(part) { return str.indexOf(part)>-1; });
    //Or using regex and word boundaries:
    var found = array.filter(function(part) { return str.match(new RegExp("\\b"+part+"\\b"))!=null; });
    return (found.length == array.length);
}
str_match_array(str1,words);//true
str_match_array(str2,words);//false

使用.every带有正则表达式的方法来断言单词边界:

function str_match_array(str,arr)
{
    return arr.every(function(part) {
                return str.match(new RegExp("\\b"+part+"\\b"))!=null;
            }); 
}
var result = str_match_array(str1,words);//true
var result = str_match_array(str2,words);//false
于 2013-08-09T13:28:17.790 回答
1

对,如果子字符串数组不是给定的:

words = words.forEach(function(word)
{
    return new RegExp(
        word.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"),//escape
        'i'//optional, to make matches case-insensitive
    );
});
//for browsers that don't support forEach:
words = (function(words)
{//turn array of substrings into regex's
    for (var i=0;i<words.length;i++)
    {
        words[i] = new RegExp(
               words[i].replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"),
               'i'
        );
    }
    return words;
}(words));

然后,匹配任何子字符串:

function checkAll(string, patterns)
{
    patterns.forEach(function(pattern)
    {//or for loop for older browsers...
        if (!pattern.test(string))
        {//no match found
            return false;
        }
    }
    return true;
}
checkAll('growing up my pet dog was named spot', words);//true
checkAll('growing up my pet DOG was named Spot', words);//true
checkAll('the quick brown fox jumps over the lazy dog', words);//false

我得到了 char 转义正则表达式,用于转义从这里RegExp传递给构造函数的字符串。

于 2013-08-09T13:43:38.297 回答
0

您不需要正则表达式 - 只需使用 indexOf,检查每个单词,如果没有找到任何单词则返回

var check_string = function(string, words) {
    var count = 0;
    for (var i in words) {
      if (string.indexOf(words[i]) === -1) {
         return false;
      }
    }
    return true;
}
于 2013-08-09T13:16:51.810 回答
0
var ary=['my', 'dog', 'spot'];
var str="growing up my pet dog was named spot";
var temp="";

for(var i=0;i<ary.length;i++){

    temp=temp+".*(?:"+ary[i]+").*";

}

var regex= new RegExp(temp);
alert(regex.test(str));

http://jsfiddle.net/cgYTZ/

于 2013-08-09T13:40:32.693 回答