2016-08-31 62 views
0

我的datepicker正則表達式嘗試匹配null aray。我如何解決它?不知道如果數組爲null,clazz應該等於什麼。我想一個簡單的if(匹配[1]){etc},但我不知道如果匹配爲空,該怎麼做。 Clazz在代碼中使用了兩次。我只是將clazz設置爲null或零?正則表達式TypeError:無法讀取null的屬性'1'

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

編輯:在這裏,他們使用clazz中

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

這裏的IS_REGEXP

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

雙編輯:

這裏的整體功能

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)); 
    } 

回答

1

clazz設置爲空或空字符串應該做,如果clazz是你所關心的。

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

但隨着compareWithExp,當沒有比賽可能是從整個邏輯更好地退出:

if (! matches) return; 
相關問題