Shell-like case

C

Christopher Nelson

In Bourne shell, you can do:

case ($x) in
foo*)
;;
*bar)
;;
esac

so that the first case matches any string starting with "foo", the
second any string ending in "bar", etc. In Tcl, you can:

switch -glob $x {
"foo*" {
}
"*bar" {
}
}

and accomplish the same thing. I'm struggling to do that in
JavaScript. switch seems to follow C semantics and do a full-length
match. And String.match() doesn't seem to do glob-style matching so I
can't do:

if ($x.match("foo*")) {
...

Is there a way to match on patterns in a JavaScript control structure?
 
V

VK

In Bourne shell, you can do:

case ($x) in
foo*)
;;
*bar)
;;
esac

so that the first case matches any string starting with "foo", the
second any string ending in "bar", etc. In Tcl, you can:

switch -glob $x {
"foo*" {
}
"*bar" {
}
}

and accomplish the same thing. I'm struggling to do that in
JavaScript. switch seems to follow C semantics and do a full-length
match. And String.match() doesn't seem to do glob-style matching so I
can't do:

if ($x.match("foo*")) {
...

Is there a way to match on patterns in a JavaScript control structure?

If I understood the objective correctly - thus to get "optional fall-
through" rather than separate if-else branches then:

var probe = 'foobar';

switch (true) {
case /^foo*/.test(probe) :
window.alert('Executing statement 1');

// other cases

case /bar$/.test(probe) :
window.alert('Executing statement 2');

break; default:
window.alert('No match found');
}
 
C

Christopher Nelson

If I understood the objective correctly - thus to get "optional fall-
through" rather than separate if-else branches then:

No, my objective is glob-style or RE-style matching against the string
rather than exact matches.
var probe = 'foobar';

switch (true) {
case /^foo*/.test(probe) :
window.alert('Executing statement 1');

// other cases

case /bar$/.test(probe) :
window.alert('Executing statement 2');

break; default:
window.alert('No match found');

}

But your example seems to accomplish my goal. switch(true) is a
rather unusual construct I'd not likely have come up with soon.
Thanks.
 
N

news

No, my objective is glob-style or RE-style matching against the string
rather than exact matches.

You'll need a break; statement here or strings
starting with foo will execute both statement 1
and statement 2. VK did this deliberately as
he thought that was what you wanted, or at least
I think that is what "to get "optional fall-through""
means...
 
C

Christopher Nelson

You'll need a break; statement here or strings
starting with foo will execute both statement 1
and statement 2. VK did this deliberately as
he thought that was what you wanted, or at least
I think that is what "to get "optional fall-through""
means...
...

Yes, I think that's what he/she meant and no, that's not what I want
and I've got the break statements.
 
L

Lee

Christopher Nelson said:
String.match() doesn't seem to do glob-style matching so I
can't do:

if ($x.match("foo*")) {
...

if ( /foo.*/.test(x) ) {
...

Note that the regular expression /foo*/ matches "fo" followed
by any number of additional "o"s. It will not match "foob".


--
 
V

VK

You'll need a break; statement here or strings
starting with foo will execute both statement 1
and statement 2. VK did this deliberately as
he thought that was what you wanted, or at least
I think that is what "to get "optional fall-through""
means...

Yes, this is what I meant.
"optional entry point for fall-through statements" would be more clear
maybe. Overall I'm struggling for a while to find a clear nominative
definitions - so noun or gerundive based like "foobarish foobar" or
"foobarish foobaring" - for switch and if-else blocks highlighting
their default behavior difference.

P.S. As it was not OP's objective then if-else if-else could be more
convenient rather than seal each case by break; but it seems failing
into personal preferences.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top