正則表達式檢測ip是否合法

var pattern=/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
pattern.test(‘192.168.90.88’);//true
pattern.test(‘192.168.90.188’);//true
pattern.test(‘192.168.9.56’);//true
pattern.test(‘192.168.9.666’);//true
pattern.test(‘192.168.90.08’);//false
pattern.test(‘999.168.90.78’);//false
pattern.test(‘192.999.90.68’);//false
pattern.test(‘192.168.999.78’);//false
pattern.test(‘1922.168.90.78’);//false
pattern.test(‘192.1684.90.68’);//false
pattern.test(‘192.168.95.788’);//false

正則表達式爲:javascript

var pattern=/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;java

上面的表達式也等同於:覺得\d等於[0-9]正則表達式

/^((25[0-5] | 2[0-4]\d | 1\d{2} | [1-9]\d | \d)\.){3}(25[0-5] | 2[0-4]\d | 1\d{2} | [1-9]\d | \d)$/code

相關文章
相關標籤/搜索