const regex = /\b(^(http|https|wss|ws|ftp|ftps):\/\/127[.](?: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]?)|^(http|https|wss|ws|ftp|ftps):\/\/0.0.0.0|^(http|https|wss|ws|ftp|ftps):\/\/(10)([.](25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[0-9]{1,2})){3}|^(http|https|wss|ws|ftp|ftps):\/\/localhost|^(http|https|wss|ws|ftp|ftps):\/\/172[.](0?16|0?17|0?18|0?19|0?20|0?21|0?22|0?23|0?24|0?25|0?26|0?27|0?28|0?29|0?30|0?31)[.](?: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]?)|^(http|https|wss|ws|ftp|ftps):\/\/192[.]168[.](?: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]?)|^(http|https|wss|ws|ftp|ftps):\/\/169[.]254[.](?: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]?))(?:\/([789]|1?[0-9]{2}))?\b/igm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\b(^(http|https|wss|ws|ftp|ftps):\\\/\\\/127[.](?: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]?)|^(http|https|wss|ws|ftp|ftps):\\\/\\\/0.0.0.0|^(http|https|wss|ws|ftp|ftps):\\\/\\\/(10)([.](25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[0-9]{1,2})){3}|^(http|https|wss|ws|ftp|ftps):\\\/\\\/localhost|^(http|https|wss|ws|ftp|ftps):\\\/\\\/172[.](0?16|0?17|0?18|0?19|0?20|0?21|0?22|0?23|0?24|0?25|0?26|0?27|0?28|0?29|0?30|0?31)[.](?: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]?)|^(http|https|wss|ws|ftp|ftps):\\\/\\\/192[.]168[.](?: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]?)|^(http|https|wss|ws|ftp|ftps):\\\/\\\/169[.]254[.](?: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]?))(?:\\\/([789]|1?[0-9]{2}))?\\b', 'igm')
const str = `# loopback
http://127.0.0.0:80
https://127.0.0.0000 # only three digits supported here
# 0.0.0.0 Day
http://0.0.0.0
ws://0.0.0.0
# Localhost
http://localhost:25
https://localhost:8080
HTTP://LoCALhosT:99 # Case insensitive
ws://localhost:7777
wss://LOCALHOST:9200
https://google.com/blah/localhost/ # This should not alert
# Web Sockets
ws://localhost:2121/sockjs-node
wss://localhost:5882/
# FTP
ftp://192.168.0.1
ftps://127.0.0.1
ftps://localhost
# A (/8 CIDR)
wss://10.0.0.1/10
ws://10.255.22.33
# B (/12 CIDR)
http://172.17.100.155/32 # 172.16.0.0 to 172.31.255.255
http://172.15.1.1 # This should not fire
http://172.32.1.1 # This should not fire
# C (/16 CIDR)
https://192.168.1.1/random/path
wss://192.168.1.255
http://192.168.9.9
# IPv6
http://fc00:833e:d648:f196:5c6c:1d9a:a14b:0d59 # Private IPv6
wss://fc01:67e5::6a66:34a7
ftp://FC02:5fcf:e093:bb10:ce77:b5c9::/112
http://fc03:49bd:b7c1:5685:fa0a:87e9::2/128
ws://fc04:::2 hmpf
http://0de9:b022:883f:2c3c:7dba:a184:7576:4531 # public IP addresses
https://04e4:e53b:0c2f:2990:27ad:0464:6dd3:b6b3
wss://0319:3b4a:546e:d480:4814:f885:3396:bba2
ws://5394:03f5:606f:273e:e343:d94a:610a:3f2e
ftp://63cd:ad1f:7ffb:62c7:cc17:6e20:9b59:2f7d
# IPV6 with brackets
http://[::1] # Private IPv6
http://[fc00:833e:d648:f196:5c6c:1d9a:a14b:0d59]
wss://[fc01:67e5::6a66:34a7]
ftp://[FC02:5fcf:e093:bb10:ce77:b5c9::]/112
http://[fc03:49bd:b7c1:5685:fa0a:87e9::2]/128
ws://[fc04:::2] hmpf
http://[0de9:b022:883f:2c3c:7dba:a184:7576:4531] # public IP addresses None of these should fire
https://[04e4:e53b:0c2f:2990:27ad:0464:6dd3:b6b3]
wss://[0319:3b4a:546e:d480:4814:f885:3396:bba2]
ws://[5394:03f5:606f:273e:e343:d94a:610a:3f2e]
ftp://[63cd:ad1f:7ffb:62c7:cc17:6e20:9b59:2f7d]
# More "crap" should not fire
ws://30.168.1.255.1
wss://127.1
https://192.168.1.256
http://-1.2.3.4
wss://1.1.1.1.
ftp://3...3
wss://255.255.255.255
ftps://0.0.0.0
wss://1.1.1.01
https://10207287.fls.doubleclick.net/
# decimal vs. octal mix
http://172.031.33.33
http://172.021.133.01
# octal - not supported here
http://0254.0021.0062.0041 # Should not fire
# link-local addresses IPv4
http://169.254.1.0
http://169.254.254.255
# link-local addresses IPv6
http://fe90:1234::/64
http://feaf:ffff::/128
http://febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions