2012-01-12 37 views
3

我試圖得到一個簡單的正則表達式來驗證URL包含結局數字正則表達式匹配圖片網址

var testRegex = /^https?:\/\/(?:[a-z\-]+\.)+[a-z]{2,6}(?:\/[^\/#?]+)+\.(?:jpe?g|gif|png)$/; 
var imageUrl = "http://stackoverflow.com/questions/406192/how-to-get-the-current-url-in-jquery"; 
if (testRegex.test(imageUrl)) { 
    alert('Not Match'); 
} 

應該觸發alert('Not Match');,它不?見http://jsfiddle.net/UgfKn/

發生了什麼事?

回答

3

你的意思是:

if (!testRegex.test(imageUrl)) { 
    alert('Not Match'); 
} 
+0

啊哈哈愚蠢的錯誤! :) - 至少對於圖片網址,正則表達式看起來是否正常? – Andy 2012-01-12 12:13:31

+0

在正則表達式後追加小寫i:'/^https?:\/\ /(?:[az \ - ] + \。)+ [az] {2,6}(?:\/[^ \ /#?] +)+ \。(?: jpe?g | gif | png)$/i',如果是大寫字母。 – itea 2012-01-12 12:20:58

+0

啊非常感謝:) – Andy 2012-01-12 12:27:26

1

testRegex.test將評估爲True,如果testRegex匹配。 即

if (testRegex.test(imageUrl)) { 
    alert('Match'); 
} else { 
    alert('Not match'); 
}