2015-06-19 44 views
2

我需要將一些url作爲{{vars}}傳遞給某些iframes src's。

域名是:

1 : http://chateauversailles.fr 
2 : http://www.chateauversailles-spectacles.fr/ 


這適用於所有的#1網址:

$sceDelegateProvider.resourceUrlWhitelist(['self', new RegExp('^(http[s]?):\/\/(w{3}.)?chateauversailles\.fr/.+$')]); 


當我嘗試薩姆斯域#2法則:

$sceDelegateProvider.resourceUrlWhitelist(['self', new RegExp('^(http[s]?):\/\/(w{3}.)?chateauversailles\.fr/.+$')]); 
$sceDelegateProvider.resourceUrlWhitelist(['self', new RegExp('^(http[s]?):\/\/(w{3}.)?chateauversailles\-spectacles\.fr/.+$')]); 

它打破了所有iframe src的,包括域名#1調用通常$ sce錯誤:[$sce:insecurl] Blocked loading resource from url not allowed by $sceDelegate policy


如果我嘗試在#1之前放置語句#2,則域#1的URL被列入白名單,但不是#2。

我懷疑罪魁禍首是正則表達式,但他們對我來說看起來相當不錯,但對於$ sce顯然還不夠。

+0

嘗試'$ sceDelegateProvider.resourceUrlWhitelist(['self',new RegExp('^(https?):\\/\\ /(w {3} \\。)?chateauversailles(-spectacles)?\ \ .fr /.+$')]);'作爲單個語句。 –

+0

@stribizhev - 就是那個。謝謝。請將您的評論轉換爲答案,以便我可以讓您滿意。 –

+0

已發佈。很高興它變成爲你工作。 –

回答

3

可以「合併」兩個語句爲一個使用可選組:

$sceDelegateProvider.resourceUrlWhitelist(['self', new RegExp('^(https?):\\/\\/(w{3}\\.)?chateauversailles(-spectacles)?\\.fr/.+$')]); 

請注意,你需要使用一個RegExp構造函數時使用雙轉義。

正則表達式的說明

  • ^ - 啓動串錨
  • (https?) - 捕獲組匹配httphttps(請注意,你可以,如果你不使用反向引用刪除圓括號)
  • :\\/\\/ - 從字面上匹配://(注意,您不必在構造函數中跳過斜線,因爲它不允許使用分隔符,因此您可以編寫://
  • (w{3}\\.)? - (因爲?量詞),該信w,恰好3次出現和文字點
  • chateauversailles匹配的可選捕獲組 - 匹配指定字母序列1周時間
  • (-spectacles)? - 可選組匹配-spectacles或由於?量詞而沒有任何結果。
  • \\.fr/ - 匹配.fr/字面上
  • .+ - 匹配比換行符
  • $其他任何1個或多個字符 - 字符串的結尾。
+0

多麼好的,詳細的答案。一個很好的正則表達式教程。 –

+0

歡迎您:) –

相關問題