url-regex-safe-deno
These include codes under the LICENSE code:mod.ts
/* istanbul ignore next */
const ipv4 = ipRegex.v4().source;
const ipv6 = ipRegex.v6().source;
export type Options = Partial<{
exact: boolean;
strict: boolean;
auth: boolean;
localhost: boolean;
parens: boolean;
apostrophes: boolean;
trailingPeriod: boolean;
ipv4: boolean;
ipv6: boolean;
tlds: string[];
returnString: boolean;
}>;
export const urlRegexSafe = (options_: Options = {}) => {
const options = {
exact: false,
strict: false,
auth: false,
localhost: true,
parens: false,
apostrophes: false,
trailingPeriod: false,
ipv4: true,
ipv6: true,
tlds,
returnString: false,
...options_
};
const protocol = (?:(?:[a-z]+:)?//)${options.strict ? '' : '?'};
// Add option to disable matching urls with HTTP Basic Authentication
const auth = options.auth ? '(?:\\S+(?::\\S*)?@)?' : '';
const domain =
// Add ability to pass custom list of tlds
const tld = `(?:\\.${
options.strict
: (?:${options.tlds.sort((a, b) => b.length - a.length).join('|')})
})${options.trailingPeriod ? '\\.?' : ''}`;
const port = '(?::\\d{2,5})?';
// Not accept closing parenthesis
// Don't allow apostrophes
const path = options.parens
? options.apostrophes
: options.apostrophes
// Added IPv6 support
let regex = (?:${protocol}|www\\.)${auth}(?:;
if (options.localhost) regex += 'localhost|';
if (options.ipv4) regex += ${ipv4}|;
if (options.ipv6) regex += ${ipv6}|;
regex += ${host}${domain}${tld})${port}${path};
// Add option to return the regex string instead of a RegExp
//if (options.returnString) return regex;
return options.exact
? new RegExp((?:^${regex}$), 'i')
: new RegExp(regex, 'ig');
};