Wildcard string comparison in Javascript

后端 未结 7 2177
花落未央
花落未央 2020-12-01 06:00

Let\'s say I have an array with many Strings Called \"birdBlue\", \"birdRed\" and some other animals like \"pig1\", \"pig2\"

7条回答
  •  没有蜡笔的小新
    2020-12-01 06:45

    var searchArray = function(arr, str){
        // If there are no items in the array, return an empty array
        if(typeof arr === 'undefined' || arr.length === 0) return [];
        // If the string is empty return all items in the array
        if(typeof str === 'undefined' || str.length === 0) return arr;
    
        // Create a new array to hold the results.
        var res = [];
    
        // Check where the start (*) is in the string
        var starIndex = str.indexOf('*');
    
        // If the star is the first character...
        if(starIndex === 0) {
    
            // Get the string without the star.
            str = str.substr(1);
            for(var i = 0; i < arr.length; i++) {
    
                // Check if each item contains an indexOf function, if it doesn't it's not a (standard) string.
                // It doesn't necessarily mean it IS a string either.
                if(!arr[i].indexOf) continue;
    
                // Check if the string is at the end of each item.
                if(arr[i].indexOf(str) === arr[i].length - str.length) {                    
                    // If it is, add the item to the results.
                    res.push(arr[i]);
                }
            }
        }
        // Otherwise, if the star is the last character
        else if(starIndex === str.length - 1) {
            // Get the string without the star.
            str = str.substr(0, str.length - 1);
            for(var i = 0; i < arr.length; i++){
                // Check indexOf function                
                if(!arr[i].indexOf) continue;
                // Check if the string is at the beginning of each item
                if(arr[i].indexOf(str) === 0) {
                    // If it is, add the item to the results.
                    res.push(arr[i]);
                }
            }
        }
        // In any other case...
        else {            
            for(var i = 0; i < arr.length; i++){
                // Check indexOf function
                if(!arr[i].indexOf) continue;
                // Check if the string is anywhere in each item
                if(arr[i].indexOf(str) !== -1) {
                    // If it is, add the item to the results
                    res.push(arr[i]);
                }
            }
        }
    
        // Return the results as a new array.
        return res;
    }
    
    var birds = ['bird1','somebird','bird5','bird-big','abird-song'];
    
    var res = searchArray(birds, 'bird*');
    // Results: bird1, bird5, bird-big
    var res = searchArray(birds, '*bird');
    // Results: somebird
    var res = searchArray(birds, 'bird');
    // Results: bird1, somebird, bird5, bird-big, abird-song
    

    There is an long list of caveats to a method like this, and a long list of 'what ifs' that are not taken into account, some of which are mentioned in other answers. But for a simple use of star syntax this may be a good starting point.

    Fiddle

提交回复
热议问题