Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch Case Regex Test

var path = location.pathname;
 switch(path){
  case (/\/memberlist/).test(path) :getCSS('url-22.css'); break;
  case (/\/register/).test(path):  getCSS('url-6.css');  break;
  case (/buy-credits/g).test(path): getCSS('url-7.css'); break;
  case (/\/?u(\d+)friends$/).test(path): getCSS('url-8.css'); break;
  case (/\/privmsg/).test(path): getCSS('url-9.css'); break;
  case (/\/?u(\d+)wall$/).test(path): getCSS('url-4.css'); break;
 }
  function getCSS(url,media){
       var a = document.createElement('link');
           a.href=url;
           a.media= media || 'screen';
           a.rel="stylesheet";
     return (document.getElementsByTagName('head')[0].appendChild(a));    
  }

That is my code, and for some reason it's not running the function that should run. For testing purpose we could change var path="/memberlist" and it still won't run. Can someone explain to me why this won't run. Don't really use switch statements

like image 637
EasyBB Avatar asked Feb 11 '14 06:02

EasyBB


People also ask

Can we use regular expression in switch case?

RegExp can be used on the input string with the match method too. To ensure that we have a match in a case clause, we will test the original str value (that is provided to the switch statement) against the input property of a successful match .

Is a switch statement faster than if?

A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.

What does this regex do?

Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions. However, its only one of the many places you can find regular expressions.

How do you match a pattern in JavaScript?

RegExp Object A regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.


3 Answers

None of the answers posted show a correct method to use a RegExp pattern in a switch case so I thought I'd post:

switch (myVar) {
   case 'case1':
      /...do work
      break
   case /[a-z]*/.test(myVar) && myVar:
      /...regex match, do work
      break
}
like image 139
Cory Robinson Avatar answered Sep 30 '22 07:09

Cory Robinson


change

switch(path){

to

switch(true){

as you can see in thread I'm reffering to in comment.

like image 27
chillworld Avatar answered Sep 30 '22 07:09

chillworld


Just for the record, the switch case could be rewritten to:

getCSS(
    /\/memberlist/).test(path)    && 'url-22.css' ||
    /\/register/).test(path)      && 'url-6.css'  ||
    /buy-credits/g).test(path)    && 'url-7.css'  ||
    /\/?u(\d+)friends$/)          && 'url-8.css'  ||
    /\/privmsg/).test(path)       && 'url-9.css'  ||
    /\/?u(\d+)wall$/).test(path)  && 'url-4.css'  || 
    'default'
);

Or rewrite getCSS, using a helper object

var path2url = {
   css: [
                {re: /\/register/, css: 'url-22.css'},
                {re: /buy-credits/g, css: 'url-6.css'},
                {re: /\/?u(\d+)friends$/, css: 'url-8.css'},
                {re: /\/privmsg/, css: 'url-8.css'},
                {re: /\/?u(\d+)wall$/, css: 'url-4.css'}
        ],
   getURL: function(path) {
     var i = this.css.length;
     while (--i) {
       if (this.css[i].re.test(path)) {
         return this.css[i].css;
       }
     }
     return null; // or something default
   }
};

function getCSS(path,media){
  var a = document.createElement('link');
  a.href= path2url.getURL(path); // <=
  a.media= media || 'screen';
  a.rel="stylesheet";
  return (document.getElementsByTagName('head')[0].appendChild(a));    
}
like image 21
KooiInc Avatar answered Sep 30 '22 07:09

KooiInc