const regex = /^(?:[a-z][0-9a-z-.+]*:\/\/)?((?:(?:[a-z\u00a1-\uffff0-9_]+-?)*[a-z\u00a1-\uffff0-9_]+)(?:\.(?:[a-z\u00a1-\uffff0-9_]+-?)*[a-z\u00a1-\uffff0-9_]+)*(?:\.(?:[a-z0-9\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/.*)?(?:\?.*)?(?:\#.*)?$/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?:[a-z][0-9a-z-.+]*:\\\/\\\/)?((?:(?:[a-z\\u00a1-\\uffff0-9_]+-?)*[a-z\\u00a1-\\uffff0-9_]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9_]+-?)*[a-z\\u00a1-\\uffff0-9_]+)*(?:\\.(?:[a-z0-9\\u00a1-\\uffff]{2,})))(?::\\d{2,5})?(?:\\\/.*)?(?:\\?.*)?(?:\\#.*)?$', 'gmi')
const str = `socks://www.example.com
10.1.1.1
10.1.1.2/4
10.1.1.3/0
10.1.1.0/8
10.1.1.5/32
10.1.1.6/35
10.1.1.7/36000
10.1.1.8/abcd
10.1.1.9-10.1.1.10
user:pass@zxuz.com
user@proxy.brew.opendnstest.com/customBlockUserInfo
user:@proxy.brew1.opendnstest.com/customBlockUserInfo
user:password@proxy.brew2.opendnstest.com/customBlockUserInfo
ftp://cnn.example.com&story=breaking_news@foobar.com/top_story.htm
foo.com:8080/bar/baz
asdfasdf.com/foo/b%20ar
https://example.com/archive/*/http://somesite.com
`;
// 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