文字列の配列が与えられた場合:
x = ["banana","apple","orange"]
ワイルドカード検索を実行するための組み込みのショートカットはありますか?
つまり、多分
x.indexOf("*na*") //returns index of a string containing the substring na
文字列の配列が与えられた場合:
x = ["banana","apple","orange"]
ワイルドカード検索を実行するための組み込みのショートカットはありますか?
つまり、多分
x.indexOf("*na*") //returns index of a string containing the substring na
Pimの答えを拡張すると、(jQueryなしで)それを行う正しい方法は次のようになります。
Array.prototype.find = function(match) {
return this.filter(function(item){
return typeof item == 'string' && item.indexOf(match) > -1;
});
}
filter
しかし実際には、この機能を複数の場所で使用しているのでない限り、既存の方法を使用することができます。
var result = x.filter(function(item){
return typeof item == 'string' && item.indexOf("na") > -1;
});
RegExpバージョンも同様ですが、オーバーヘッドが少し増えると思います。
Array.prototype.findReg = function(match) {
var reg = new RegExp(match);
return this.filter(function(item){
return typeof item == 'string' && item.match(reg);
});
}
ただし、有効なRegExp文字列を指定できる柔軟性があります。
x.findReg('a'); // returns all three
x.findReg("a$"); // returns only "banana" since it's looking for 'a' at the end of the string.
@Shmiddtyの回答を拡張して、JavaScriptの便利なアイデアを以下に示します。
Array.prototype.method = function(arg) { return result; }
Array.filter(function(e) { return true|false; })
Array.map(function(e) { return formula(e); })
/.*na.*/
またはnew Regex('.*na.*')
let result = regex.test(input);
つまり、入力引数を正規表現にすることをお勧めします。したがって、次のいずれかが得られます。
解決策1:フィルター、テスト、マップ、およびindexOf
Array.prototype.find = function(regex) {
const arr = this;
const matches = arr.filter( function(e) { return regex.test(e); } );
return matches.map(function(e) { return arr.indexOf(e); } );
};
let x = [ "banana", "apple", "orange" ];
console.log(x.find(/na/)); // Contains 'na'? Outputs: [0]
console.log(x.find(/a/)); // Contains 'a'? Outputs: [0,1,2]
console.log(x.find(/^a/)); // Starts with 'a'? Outputs: [1]
console.log(x.find(/e$/)); // Ends with 'e'? Outputs: [1,2]
console.log(x.find(/pear/)); // Contains 'pear'? Outputs: []
解決策2:削減、テスト
Array.prototype.find = function(regex) {
return this.reduce(function (acc, curr, index, arr) {
if (regex.test(curr)) { acc.push(index); }
return acc;
}, [ ]);
}
let x = [ "banana", "apple", "orange" ];
console.log(x.find(/na/)); // Contains 'na'? Outputs: [0]
console.log(x.find(/a/)); // Contains 'a'? Outputs: [0,1,2]
console.log(x.find(/^a/)); // Starts with 'a'? Outputs: [1]
console.log(x.find(/e$/)); // Ends with 'e'? Outputs: [1,2]
console.log(x.find(/pear/)); // Contains 'pear'? Outputs: []
アレイのプロトタイプを拡張して、アレイ内の一致を見つけることができます
Array.prototype.find = function(match) {
var matches = [];
$.each(this, function(index, str) {
if(str.indexOf(match) !== -1) {
matches.push(index);
}
});
return matches;
}
次に、次のように配列でfindを呼び出すことができます。
// returns [0,3]
["banana","apple","orange", "testna"].find('na');
正規表現を使用すると、JavaScriptでこれを行うことができます
var searchin = item.toLowerCase();
var str = columnId;
str = str.replace(/[*]/g, ".*").toLowerCase().trim();
return new RegExp("^"+ str + "$").test(searchin);
言われている他のすべてに加えて、あなたはこれを行うことができます:
var x = ["banana", "apple", "orange"];
var y = [];
for (var i in x) {
if (x[i].indexOf('na') > -1) {
y.push(i);
}
}
結果:y = [0]