www.webdeveloper.com
Results 1 to 3 of 3

Thread: RegEx Switch Case?

  1. #1
    Join Date
    Dec 2008
    Location
    The Netherlands
    Posts
    2

    RegEx Switch Case?

    Hello, I am new to these forums.
    I am a Coder / Security Guy. And Normally I only code stuff on low Level (Assembler / ANSI C)
    But my work also involves some scripting. Normally in Perl on Linux and in C# on the Windows Platform.

    I am known with Javascript the way nearly all the people like me are.
    I know the basics. Since I need it sometimes for XSS-Proof-of-Concepts.

    But as I am not needed to do my normal work now they asked me to write a small part of javascript for the CRM System they run.

    So I did this:

    Code:
    	function postalcode (country, postalcode)
    	{
    		var myregexp;
    		switch (country)
    		{
    			case RegExp('^31'): /* The Netherlands */
    			   myregexp = /^\d{4}\D{2}$/;
    			   break;
    			case '^44': /* United Kingdom */
    				myregexp = /^\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b}$/;
    			   break;
    			default:
    				incorrect( "Invalid Country" );
    		}
    		if (!postalcode.match( myregexp )) { incorrect( "Invalid PostalCode" ); }
    	}
    The country Parameter is given in a Bigger string of Numbers.
    So its 3112156161 or 4415654654 the first two always being the Country code.
    So, I am trying to do this:
    Code:
    case RegExp('^31'): /* The Netherlands */
    But that doesn't work and I kinda see why. But is there a way to make something like this work?

    I did do some Googling but I didn't really find anything.
    Oh, The if-elseif would not really work as there are more than 27 Countrys in the list. And it would make the code Quite Messy.

    So is there anyone out there at WebDeveloper land that might be able to help me with my Lack of Javascript Knowledge?

    Thanks!

    Kind regards,
    Robin


    [EDIT:]

    I already have a fix but I want to know if there is a way to do it in the switch case anyway
    Now I use a Replace Function. But I still want to do it differently..
    Last edited by RVossen; 12-04-2008 at 03:47 AM. Reason: Not Complete..

  2. #2
    Join Date
    Nov 2008
    Posts
    157
    I was thinking that this isnt possible to do reliably, but then I changed my mind - there are probably lots of solutions.
    The problem is that in a switch statement with 'case x' the value of x must match the value of the variable we are switching on (as i`m sure you know).
    Most regex functions return arrays of matching sets etc, none of the built in javascript regex functions return the full string if it matches. (you could easily write a function to do that then you could do the switch how you wanted).
    heres what i did though using RegExp.test(myString)
    Code:
    switch(true){
    	case /^44/.test(str): alert("UK");
    	break;
    	case /^31/.test(str) : alert("Netherlands");
    	break;
    	default: alert("fail");
    }
    Alternatively use the substring function to get the bit of the string you need to check and this to me is more logical:
    Code:
    switch(str.substring(0,2)){
    	case "44": alert("UK");
    	break;
    	case "31": alert("Netherlands");
    	break;
    	default: alert("fail");
    }
    Hope one of these is useful
    Last edited by mintedjo; 12-04-2008 at 04:43 AM.

  3. #3
    Join Date
    Dec 2008
    Location
    The Netherlands
    Posts
    2
    Well thanks case /^44/.test(str): alert("UK"); that looks great =) I will use that..
    I have it now kinda the way you did in the second one. But there is a reason I want it to be RegExed. That way we can change it later.

    But yea thanks I'll try that =)

    Cheers,
    Robin

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles