パターンが文字列内の文字列(大文字と小文字は区別されます)に表示されている場合はtrueを返しますが、パターンのすべての文字が(順序にかかわらず)文字列で表示される場合は、その機能を拡張して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 allChars = what.split( '');' - 行は何をしますか? – methuselah
@jeansymolanza:この行は、文字列(変数 '' '' ')を個々の文字に分割します(空の文字列' '''をパラメータとして渡した場合、 'split()'メソッドはそのように動作します) allChars'」を参照してください。十分ですか? – Tadeck