Can I use wildcards when searching for an array of strings in Javascript?

Given an array of strings:

x = ["banana","apple","orange"] 

Is there a built-in shortcut for performing a pattern search?

i.e. maybe

 x.indexOf("*na*") //returns index of a string containing the substring na 
+11
javascript
source share
5 answers

Turning around to Pim's answer, the correct way to do this (without jQuery) would be this:

 Array.prototype.find = function(match) { return this.filter(function(item){ return typeof item == 'string' && item.indexOf(match) > -1; }); } 

But really, if you do not use this function in several places, you can simply use the existing filter method:

 var result = x.filter(function(item){ return typeof item == 'string' && item.indexOf("na") > -1; }); 

The RegExp version is similar, but I think it will create a little more utility:

 Array.prototype.findReg = function(match) { var reg = new RegExp(match); return this.filter(function(item){ return typeof item == 'string' && item.match(reg); }); } 

It provides the flexibility to let you specify a valid RegExp string.

 x.findReg('a'); // returns all three x.findReg("a$"); // returns only "banana" since it looking for 'a' at the end of the string. 
+10
source share

@Shmiddty answer extension, here are some useful JavaScript ideas:

  • Extend the array with the new method: Array.prototype.method = function(arg) { return result; } Array.prototype.method = function(arg) { return result; }
  • Filter matrices using: Array.filter(function(e) { return true|false; })
  • Apply the formula to the elements in the array: Array.map(function(e) { return formula(e); })
  • Use regular expressions: either /.*na.*/ or new Regex('.*na.*')
  • Use regular expressions to match: var result = regex.test(input);
  • Use Array.prototype.reduce to aggregate the result after running the function for each element of the array

i.e. I prefer the input argument to be a regular expression, so it gives you:

  • Short but universal input matching pattern
    • eg. contains, starts with, ends with width, as well as more complex matches
  • Ability to specify an input pattern as a string

SOLUTION 1: filter, test, map and indexOf

 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: [] 

SOLUTION 2: reduce, check

 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: [] 
+3
source share

You can expand the prototype of the array to find matches in the array

 Array.prototype.find = function(match) { var matches = []; $.each(this, function(index, str) { if(str.indexOf(match) !== -1) { matches.push(index); } }); return matches; } 

Then you can call find in your array like this

 // returns [0,3] ["banana","apple","orange", "testna"].find('na'); 
+1
source share

Using regex can do this in javascript

 var searchin = item.toLowerCase(); var str = columnId; str = str.replace(/[*]/g, ".*").toLowerCase().trim(); return new RegExp("^"+ str + "$").test(searchin); 
0
source share

In addition to all that has been said, you can do this:

 var x = ["banana", "apple", "orange"]; var y = []; for (var i in x) { if (x[i].indexOf('na') == -1) { y.push(x[i]); } } x = y; 

You do not want the .splice() array through which you go through the loop, as this will change the length of the array and affect the loop. But you can loop through and create a second array with only the values ​​you want to save, and then write a new one on top of the original array.

This is no better than the other answers, it is just another way to do the same.

0
source share

All Articles