![](/img/trans.png)
[英]Replace all digit and asterisk characters except at start of string using JavaScript
[英]ASterisk or digit in regex using javascript
我的意圖是使用正則表達式來驗證以下內容
10.10.*.1
10.10.0.1
10.10.255.1
10.10.10.*
10.10.10.0
10.10.10.255
簡單來說,星號可以出現在最后兩個八位字節中,並且IP地址仍然有效
我的代碼如下:
function ValidateIPaddress(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|\*?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|\*?)$/.test(ipaddress)) {
return (true)
}
return (false)
}
我無法在最后兩個八位位組中設置or條件。 請幫忙
無需過於復雜的正則表達式。 只需確保該字符串符合通用格式(以下正則表達式),然后測試每個八位位組< 256
,如下代碼所示即可。
^\d{1,3}\.\d{1,3}(?:\.(?:\d{1,3}|\*)){2}$
^
在行首處聲明位置 \\d{1,3}
匹配一個數字1至3次 \\.
匹配.
按照字面 \\d{1,3}
匹配一個數字1至3次 (?:\\.(?:\\d{1,3}|\\*)){2}
匹配以下兩次
\\.
匹配.
按照字面 (?:\\d{1,3}|\\*)
匹配數字1至3次或*
字面值\\ $
在行尾聲明位置 var a = [ // valid "10.10.*.1", "10.10.0.1", "10.10.255.1", "10.10.10.*", "10.10.10.0", "10.10.10.255", // invalid "256.1.1.1", "*.1.1.1", "1.1.1", "1.1.1.1.1" ] var r = /^\\d{1,3}\\.\\d{1,3}(?:\\.(?:\\d{1,3}|\\*)){2}$/ a.forEach(function(ip) { console.log(`${ip}: ${r.test(ip) && ip.split('.').every(function(x) { return Number(x) < 256 || x === '*' })}`) })
另外,甚至不使用正則表達式:
var a = [ // valid "10.10.*.1", "10.10.0.1", "10.10.255.1", "10.10.10.*", "10.10.10.0", "10.10.10.255", // invalid "256.1.1.1", "*.1.1.1", "1.1.1", "1.1.1.1.1" ] a.forEach(function(ip) { var octets = ip.split('.'), valid = false if(octets.length === 4) { if(Number(octets[0]) < 256 && Number(octets[1]) < 256 && (Number(octets[2]) < 256 || octets[2] === '*') && (Number(octets[3]) < 256 || octets[3] === '*') ) valid = true } console.log(`${ip}: ${valid}`) })
這是最腫的方法,但這似乎正是您要尋找的方法。
^(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2}|\*)){2}$
var a = [ // valid "10.10.*.1", "10.10.0.1", "10.10.255.1", "10.10.10.*", "10.10.10.0", "10.10.10.255", // invalid "256.1.1.1", "*.1.1.1", "1.1.1", "1.1.1.1.1" ] var r = /^(?:25[0-5]|2[0-4]\\d|[01]?\\d{1,2})\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(?:\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d{1,2}|\\*)){2}$/ a.forEach(function(ip) { console.log(`${ip}: ${r.test(ip)}`) })
您在最后兩個八位位組上缺少任何捕獲一位數可能性的東西。 嘗試:
^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|[0-9]|\*?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|[0-9]|\*?)$
我認為應該這樣做。
let rx = /^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]|\\*)\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]|\\*)$/; const test = str => rx.test(str) console.log("these should pass") console.log(test("10.10.*.1")) console.log(test("10.10.0.1")) console.log(test("10.10.255.1")) console.log(test("10.10.10.*")) console.log(test("10.10.10.0")) console.log(test("10.10.10.255")) console.log("------------------------------") console.log("these should fail") console.log(test("10.267.0.1")) console.log(test("10.10.0.1.0.1")) console.log(test("10.*.0.1")) console.log(test("*.42.0.1"))
但是,正如@ctwheels指出的那樣,使用較少的正則表達式可能會更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.