2016-08-31 14 views
0

Mein regulärer Ausdruck für datepicker versucht Übereinstimmungen in einem Nullarray. Wie repariere ich es? Nicht sicher, was clazz gleich sein soll, wenn das Array null ist. Ich denke ein einfaches if (entspricht [1]) {etc}, aber ich bin nicht sicher, was zu tun ist, wenn Matches null ist. Clazz wird an anderer Stelle zweimal im Code verwendet. Setze ich clazz einfach auf null oder null?Regex TypeError: Kann die Eigenschaft '1' von null nicht lesen

var matches = exp.match(IS_REGEXP); 
    var clazz = scope.$eval(matches[1]); 

Edit: Hier ist, wo sie clazz verwenden

if (data.lastActivated !== newActivated) { 
     if (data.lastActivated) { 
     $animate.removeClass(data.lastActivated.element, clazz); 
     } 
     if (newActivated) { 
     $animate.addClass(newActivated.element, clazz); 
     } 
     data.lastActivated = newActivated; 
    } 

Hier IS_REGEXP

     11111111   22222222 
    var IS_REGEXP = /^\s*([\s\S]+?)\s+for\s+([\s\S]+?)\s*$/; 

Doppel Edit:

Hier ist die ganze Funktion

function addForExp(exp, scope) { 
    var matches = exp.match(IS_REGEXP); 

     var clazz = scope.$eval(matches[1]); 
    var compareWithExp = matches[2]; 
    var data = expToData[exp]; 
    if (!data) { 
     var watchFn = function(compareWithVal) { 
     var newActivated = null; 
     instances.some(function(instance) { 
      var thisVal = instance.scope.$eval(onExp); 
      if (thisVal === compareWithVal) { 
      newActivated = instance; 
      return true; 
      } 
     }); 
     if (data.lastActivated !== newActivated) { 
      if (data.lastActivated) { 
      $animate.removeClass(data.lastActivated.element, clazz); 
      } 
      if (newActivated) { 
      $animate.addClass(newActivated.element, clazz); 
      } 
      data.lastActivated = newActivated; 
     } 
     }; 
     expToData[exp] = data = { 
     lastActivated: null, 
     scope: scope, 
     watchFn: watchFn, 
     compareWithExp: compareWithExp, 
     watcher: scope.$watch(compareWithExp, watchFn) 
     }; 
    } 
    data.watchFn(scope.$eval(compareWithExp)); 
    } 

Antwort

1

Einstellung clazz auf Null oder leere Zeichenfolge soll tun, wenn Clazz Ihre Sorge ist.

var clazz = matches ? scope.$eval(matches[1]) : ''; 

Aber mit compareWithExp, könnte es besser sein Ausgang aus der ganzen Logik, wenn es keine Übereinstimmung gibt:

if (! matches) return; 
Verwandte Themen