Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    Using regular expressions to search an array of numbers?

    Hi:
    Lets say I have an array containing a set of numbers:

    var possibleids=[2, 23, 4, 5, 8]

    and I wish to scan through this array using regular expressions to see if one of them matches the number contained in another variable:

    var myid=2

    In other words:

    if (myid==one of possibleids)
    //do this

    What would be the easiest way using regular expressions? I could easily do this using a for loop and "indexOf()", but I wish to be a little more elegant.

    Thanks,
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    New Coder
    Join Date
    Mar 2003
    Location
    Somewhere far beyond
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function in_array(arr,num)
    {
       return RegExp(",?"+num+",?").test(arr.valueOf());
    }
    something like this. IMO loop approach is better.
    WBR, Weirdan.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Regex aren't made for searching arrays. Loop through it instead.

    Code:
    var
       i=arr.length;
    while(arr[--i]!=myid)
        if(i<0)
            break;
    alert(i+' is the number you want');
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Agreed. It think looping it still the better choice
    Code:
    Array.prototype.inArray = function( val )
    {
        var i = this.length;
        while( this[--i] != val )
            if ( i == 0 ) return false;
        return true;
    }
    
    Array.prototype.inArrayPattern = function( pattern )
    {
        var i = this.length;
        while( !pattern.test( this[--i] ) )
            if ( i == 0 ) return false;
        return true;
    }
    
    Array.prototype.indexOf = function( val )
    {
        var i = this.length;
        while( this[--i] != val )
            if ( i == 0 ) return -1;
        return i;
    }
    
    var pids=[2, 23, 4, 5, 8];
    alert( [pids.inArray( 2 ), pids.inArray( 3 )].join( "\n" ) );
    alert( [pids.inArrayPattern( /^3$/ ), pids.inArrayPattern( /^23|4$/ )].join( "\n" ) );
    alert( [pids.indexOf( 4 ), pids.indexOf( 3 )].join( "\n" ) );
    Last edited by beetle; 04-10-2003 at 04:57 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Oh, he wanted to see whether the value existed in the array at all, not it's index in the array...
    Code:
    Array.prototype.contains=function(attr){
        var
            fnFind,
            l=this.lenght;
        switch(attr instanceof RegExp){
            case true:
                fnFind=function(val){
                    return attr.test(val);
                }
                break;
            default:
                fnFind=function(val){
                    return attr==val;
                }
        }
        while(l-->0)
            if(fnFind(this[l]))
                return true;
        return false;
    }
    Code:
    Array.prototype.find=function(attr){
        var
            fnFind,
            l=this.lenght;
        switch(attr instanceof RegExp){
            case true:
                fnFind=function(val){
                    return attr.test(val);
                }
                break;
            default:
                fnFind=function(val){
                    return attr==val;
                }
        }
        while(l-->0)
            if(fnFind(this[l]))
                break;
        return l;
    }
    Last edited by liorean; 04-10-2003 at 05:12 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Thanks guys. The consensus seems to be just to just a for loop versus regular expressions. I guess there is something called "over elegance".
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •