2016-10-21 37 views
1

我試圖使用正則表達式來匹配在我的路由配置中不以「/ pt /」「/ en /」開頭的所有字符串。他們可能會或可能沒有額外的文字。

於是我想出了這一點:

$urlRouterProvider.when('^(?!/(pt|en)/).*', function ($injector, $location) { 
    const path = $location.path(); 
    $location.path('/pt' + path !== '' ? path : '/').replace(); 
}); 

不幸的是這引發錯誤:Invalid parameter name '.' in pattern '^(?!/(pt|en)/).*'

什麼是在這種情況下使用正則表達式的正確方法?

EDIT

我已經基於波希米亞響應改變的表達從'{(?!\/(pt|en)\/).*}''^(?!/(pt|en)/).*'。但錯誤仍然存​​在。

回答

1

when方法不具有$injector and $location services.use rule()代替,

更改正則表達式/^(?!\/(pt|en)).*/,以滿足您的要求。

Check the regexp here

規則()自定義URL處理

參數

處理函數,它在$injector$location服務作爲自變量的函數。您有責任以字符串的形式返回有效的路徑。

app.config(function ($urlRouterProvider) { 
    $urlRouterProvider.rule(function ($injector, $location) { 
      //what this function returns will be set as the $location.url 
      var path = $location.path(), normalized = path.toLowerCase(); 
      if ('/pt.asd'.match(/^(?!\/(pt|en)).*/) == undefined) { 
       // this starts with /pt or /en 
       // $location.replace().path(normalized); 
      } 
      else 
      { 
       // this does not starts with /pt or /en 
       // do some thing 
      } 
     }); 
}) 

Reference you are looking for