const regex = /([a-z0-9-]+\:\/+)([^\/\s]+)([a-z0-9\-@\^=%&;\/~\+]*)[\?]?([^ \#\r\n]*)#?([^ \#\r\n]*)/ig;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('([a-z0-9-]+\\:\\\/+)([^\\\/\\s]+)([a-z0-9\\-@\\^=%&;\\\/~\\+]*)[\\?]?([^ \\#\\r\\n]*)#?([^ \\#\\r\\n]*)', 'ig')
const str = `Bob: Hey there, have you checked https://www.facebook.com ?
(ignore) https://github.com/justsml?tab=activity#top (ignore this too)
smb:///winbox/dfs/ - ipp://printer
a a:// . Bob: Hey there, have you checked https://www.facebook.com ?
(ignore) https://github.com/justsml?tab=activity#top (ignore this too)
smb:///winbox/dfs/ - ipp://printer
a a:// .
chrome-extension://flags
ms-help://good-luck.html
iris.beep://really/dots // <- Matching period and dash chars at **only the beginning** of the string is difficult (unless we can anchor the beginning of line w/ ^) & would make the regex over 100 chars in length.
s3://buckets-o-fun
pkcs11://because-pkcs7-is-shit
.iris.beep://really/dots
`;
// 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