Javascript 如果模式的所有单个字符都以字符串形式出现,则返回true结果

Javascript 如果模式的所有单个字符都以字符串形式出现,则返回true结果,javascript,Javascript,我正在编写一段JS代码,如果模式作为子字符串(区分大小写)出现在字符串中,则返回true的结果,但希望扩展其功能,以便在模式的所有单个字符出现在字符串中时(无论顺序如何)返回true 例如: 这就是该计划目前所做的: match1("adipisci","pis") returns true 鉴于我现在希望它这样做: match1("adipisci","sciip") returns true match2("adipisci","sciipx") returns false because

我正在编写一段JS代码,如果模式作为子字符串(区分大小写)出现在字符串中,则返回true的结果,但希望扩展其功能,以便在模式的所有单个字符出现在字符串中时(无论顺序如何)返回true

例如:

这就是该计划目前所做的:

match1("adipisci","pis") returns true
鉴于我现在希望它这样做:

match1("adipisci","sciip") returns true
match2("adipisci","sciipx") returns false because x does not exist in variable
我很难在代码中实现这一点:

var pages=[
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
  "Nulla imperdiet laoreet neque.",
  "Praesent at gravida nisl. Quisque tincidunt, est ac porta malesuada, augue lorem posuere lacus, vitae commodo purus nunc et lacus."
  ];
var search_term = prompt('Type in search term: ','Search word');
// ask which search term the user wants to search for with default being 'Search Term' 
function find_s(w) {
    var indexes = [0,0,0] // create an array  to hold pages where search term found
    var iii = 0 // create variable to increment the number of finds in each page
    for (var ii=0;ii<pages.length;ii++) {
    // for each page in the array
        for (var i=0;i<pages[ii].length;i++) {
        // for each character in the chosen page
            if (pages[ii].substr(i,w.length).toLowerCase()==w.substr(0,w.length).toLowerCase()) {
            // check to see if the search term is there ignoring case
                iii++;
                // increment number of times the search term in found
                while(pages[ii].substr(i,1)!=" ") {
                    // move to the next word but checking for spaces
                    i++;
                    }
            }
        }
        indexes[ii]=iii;
        // update the number of times the search term is found in that page
        iii=0;
        // reset counter for next page search
    }
    return (w + " was found in this may times in each page " + indexes);
    // let the user know the result
}
alert (find_s(search_term));
var页面=[
“Lorem ipsum dolor sit amet,奉献精英。”,
“纳拉·拉奥里特·内克。”,
“在怀孕期间,我们将为您服务。我们将为您服务,为您服务,为您服务。”
];
var search_term=prompt('Type in search term:','search word');
//询问用户要搜索的搜索词,默认为“搜索词”
函数查找(w){
var indexes=[0,0,0]//创建一个数组来保存找到搜索词的页面
var iii=0//创建变量以增加每页中的查找数

对于(var ii=0;ii这将告诉您
针中的所有字符是否都存在于
干草堆中

var match1 = function(haystack, needles) {
    var chars = needles.split('');

    for (var i = 0, length = chars.length; i < length; i++) {
       if (haystack.indexOf(chars[i]) === -1) {
           return false;
       }
    }

    return true;   
}
var match1=函数(草堆、针){
var chars=针。分离(“”);
for(变量i=0,长度=chars.length;i

.

像这样的函数应该做到以下几点:

function allChars(lookIn, lookFor) {
   for(var i = 0; i < lookFor.length; ++i) {
      var c = lookFor.charAt(i);
      if(lookIn.indexOf(c) == -1)
         return false;
   }
   return true;
}
函数allChars(lookIn,lookFor){
for(变量i=0;i
实现您期望的解决方案是以下功能:

var containsChars = function(where, what){
    var containsChar = function(where, what){
        return !!(where.indexOf(what)+1);
    }    
    var allChars = what.split('');
    for (var i=0; i<allChars.length; i++){
        if (!containsChar(where, allChars[i])){
            return false;
        }
    }
    return true;
}
var containsChars=function(where,what){
var containsChar=函数(其中,什么){
return!!(where.indexOf(what)+1);
}    
var allChars=什么.分割(“”);
对于(var i=0;i


我特意分离了函数
containsChar()
,因此您可以更轻松地更改字符的比较方式(取决于您希望它们是以区分大小写的方式进行比较还是以不区分大小写的方式进行比较).

为什么它以相反的顺序返回输出?此函数只返回
true
false
。它不应该返回任何“输出”。
for(var i=0;i
No。仅在需要返回值的函数中。如果函数没有return语句,则调用时它的值为
undefined
@jeansymolanza&@BryanRoss:
return
语句还有一个原因:
return
语句停止函数的执行(因为已经提供了结果),所以它将不会返回
返回true;
如果它已经到达
返回false;
。这样,如果没有找到某个字符,函数将不会返回
true
(因为不会返回该语句)。但这些是编程基础。
var allChars=what.split(“”);
-行的作用是什么?@jeansymolanza:此行将字符串(名为“
what
”)拆分为单独的字符(
split()
方法在将空字符串作为参数传递时是这样工作的),并将其保存为名为“
allChars
”的数组变量。是否足够清晰?
var chars=pines.split(“”)
这一行的作用是什么?@jeansymolanza它将字符串中的各个字符分割成一个数组。