Using regexp in switch...case statement

Hi,

I’ve tried googling this without any luck at all - probably because the keywords show up in lots of irrelevant pages.

Can anyone tell me if there’s any way to use a regexp in the ‘case’ part of a ‘switch’ statement. I.e.:

switch ( some_variable ) {

case some_regexp :
......
break;
}

I can’t seem to find a way to do it, but i’m wondering if there’s something i’m missing.

Not sure it can be done.
Just use several IF instead of SWITCH/CASE

Yeah, thanks mate. That was the conclusion i’d come to. It would be tidier to use switch, as i need to add another ‘case’ to an existing ‘switch’ statement. But i guess it could just fall through and get caught by an ‘if’ statement.

Realistically, though, it’ll probably mean i’ll have to rewrite the whole ‘switch’ block to maintain consistency…

Make the cases be the result of the reg exp in a match or exec.
Here is one example:

function getcolorformat(s){
	var rX= /^((#[0-9a-f]{3,6})|([a-z]+)|(rgb\\([^\\)]+\\)))$/i;
	var M= rX.exec(s);
	if(!M) return false;
	switch(M[1]){
		case M[2]: return 'hex code';
		case M[3]: return 'string name';
		case M[4]: return 'rgb code';
		default: return false;
	}
}

// test cases
// getcolorformat(‘rgb(255,0,0)’);
// getcolorformat(‘#ff0000’)
// getcolorformat(‘red’)

Oh, yeah. I had an idea i needed to turn it on its head, but i couldn’t quite see how to do it.

Thanks!

I understand you will use regexp as variable?


switch ( some_variable ) {

case regexp1 :
......
break;
case regexp2 :
......
break;

}

JavaScript actually allows the cases to be any object so Regular Expressions there are perfectly valid (unlike in many other languages where what you can use as the case is more limited).

It may be “valid”, but it doesn’t appear to actually work (at least not in Firefox, anyway). I’ve tried with a regexp literal and a ‘new regexp’ object and it doesn’t match.

Can you give an example of a construct where it does work?

I suspect that it cannot be performed in that manner. A look at the documentation for the switch command helps to explain much.

I think the switch statement evaluates (switchexp == caseexp), so using a regex as a caseexp does not work as intended - it tests if the switchexp IS the regex instead of if the regex matches the switchexp. You could try:

switch (true) {
case re1.test(s):
statement
break;
case re2.test(s):
statement
break:

}

I imagine that switchexp is evaluated when entering the switch case block and that each caseexp is evaluated in turn until one is found that evaluates to the same thing as the switchexp. All following caseexp should not be evaluated.

A switch actually performs === tests not == tests. The fields have to be the same type as well as the same values.

You are right though as to why it wouldn’t work - for the case to match the value to compare it with would need to be an identical regular expression and not a string that satisfies the expression.

Being objects, two regular expressions can never be equal,
even if they have identical patterns.

alert(/[a-z]+/==/[a-z]+/) returns false.

Two regular expressions can be equal


var a = /[a-z]+/;
var b = a;
alert(a === b);

returns true since both point to the same regular expression object (and not to two separate objects that just happen to have the same pattern)

There are not two regular expressions in this code- both variables refer to the same regular expression.