给定一个字符串数组:
x = ["banana","apple","orange"]
是否有用于执行通配符搜索的内置快捷方式?
即,也许
x.indexOf("*na*") //returns index of a string containing the substring na
答案 0 :(得分:10)
扩展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.
答案 1 :(得分:3)
在@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.*')
var result = regex.test(input);
即。我更喜欢输入参数是一个正则表达式,因此,它给你:
解决方案1:过滤,测试,制图和索引
Array.prototype.find = function(regex) {
var arr = this;
var matches = arr.filter( function(e) { return regex.test(e); } );
return matches.map(function(e) { return arr.indexOf(e); } );
};
var 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: [0]
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;
}, [ ]);
}
var 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: [0]
console.log(x.find(/e$/)); // Ends with 'e'? Outputs: [1,2]
console.log(x.find(/pear/)); // Contains 'pear'? Outputs: []
答案 2 :(得分:1)
您可以扩展数组原型以查找数组中的匹配项
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');
答案 3 :(得分:0)
使用正则表达式可以在javascript中执行此操作
var searchin = item.toLowerCase();
var str = columnId;
str = str.replace(/[*]/g, ".*").toLowerCase().trim();
return new RegExp("^"+ str + "$").test(searchin);
答案 4 :(得分:0)
除了已经说过的所有其他内容,您还可以执行以下操作:
public static void CambiarCSVAXLSX(string rutaArchivo) {
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(rutaArchivo, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.SaveAs(rutaArchivo.Replace(".csv", ".xlsx"), Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
wb.Close();
app.Quit();
Microsoft.Office.Interop.Excel.Application app2 = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb2 = app2.Workbooks.Open(rutaArchivo.Replace(".csv", ".xlsx"), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet ws2 = (Worksheet)wb2.Sheets[1];
Microsoft.Office.Interop.Excel.Range xlCells = ws2.Range["1:2"];
xlCells.Select();
xlCells.NumberFormat = "mm/dd/yyyy h:mm";
wb2.SaveAs(rutaArchivo.Replace(".csv", ".xlsx"), Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
wb2.Close();
app2.Quit();
//mm/dd/yyyy h:mm
//File.Delete(rutaArchivo);
}
结果:y = [0]