admin管理员组

文章数量:1252929

If my javascript ajaxes away to my server and returns an ID of 49 in the plain text format of [49] is there a way in which i an do something like this... (i have tested and doesnt work)

switch(data)
{
    case '[*]':
        (..etc.)
    break;
}

Where the wildcard is the * and i want to make sure it is enclosed within two square parenthesis?

Because i need to check that there wasnt another word returned like error and i am reserving the default for unexpected errors, any ideas? :) Thanks!

If my javascript ajaxes away to my server and returns an ID of 49 in the plain text format of [49] is there a way in which i an do something like this... (i have tested and doesnt work)

switch(data)
{
    case '[*]':
        (..etc.)
    break;
}

Where the wildcard is the * and i want to make sure it is enclosed within two square parenthesis?

Because i need to check that there wasnt another word returned like error and i am reserving the default for unexpected errors, any ideas? :) Thanks!

Share Improve this question asked Feb 16, 2010 at 7:39 tarnfeldtarnfeld 26.6k44 gold badges112 silver badges147 bronze badges 1
  • 1 @Tracker1's answer should really be the correct one considering the title of the question. – kb. Commented Feb 16, 2010 at 8:26
Add a ment  | 

4 Answers 4

Reset to default 19

You can do a switch on true explicitely, which will use evaluation on each case statement.

switch (true) {
    case ((/^\[\d+\]$/).test(data)):
       //matches data;
       break;
    case (data == "something else"):
       //...
       break;
    default:
       //...
}

However, if you have less than say 4-5 cases, it would be better to use if/else if/else if/else blocks.

if ((/^\[\d+\]$/).test(data)) {
       //matches data;
} else if (data == "something else") {
       //...
} else {
       //...
}

I usually do some error trapping in my response methods for service/rest calls so that I almost always return a proper json with an error property if there is an error.

try {
  if (response.responseText.indexOf("<html") >= 0) { 
    throw response.responseText;
  }

  var data = JSON.parse(response.responseText);
  if (data.error) 
    throw data.error;

  //handle response data object.
  if ((/^\[\d+\]$/).test(data)) {
    //matches data;
  } else if (data == "something else") {
    //...
  } else {
    //...
  }
} catch(err) {
  if (err && err.message) {
      //use err.message
  } else if (err && err.toString().indexOf("<html") >= 0) {
      //handle error text
  }
}

You could create a list of patterns and associated callbacks and do a simple loop and check for matches. For example:

    var patterns = [];

    function myCallback(){ document.write('myCallback!'); }
    function myOtherCallback(){ document.write('myOtherCallback!'); }
    function myLastCallback(){ document.write('You will never see me!'); }

    patterns.push({'pattern':new RegExp(/\[.+\]/),'callback': myCallback});
    patterns.push({'pattern':new RegExp(/.+/),'callback':myOtherCallback});
    patterns.push({'pattern':new RegExp(/A-Z{3}/),'callback':myLastCallback});

    var f = "[49]";
    for(var i=0;i<patterns.length;i++){
        if(patterns[i].pattern.test(f)){
            patterns[i].callback();
        }
    }

Which outputs the following:

myCallback!myOtherCallback!

You could try to use if else and regex for matching wildcard patterns.

Assuming data = "[49]"; or any digits inside brackets.

if(/\[\d+\]/.test(data)){
    //do something
}else{
    //default
}

Short answer: No, switch/case can't handle wildcard.

You should probably do some preprocessing/sanity checking before entering the switch, or simply discard it pletely since it's more appropriate for specific case scenarios rather than processing streamlined data. Regexp will serve you better here.

本文标签: jqueryJavascript switch statement with wildcardStack Overflow