How to use the tlds.sort function in tlds

To help you get started, we’ve selected a few tlds examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mikesamuel / attack-review-testbed / lib / poorly-written-linkifier.js View on Github external
const replacements = {
  '<': '<',
  '>': '>',
  '"': '"',
};

function normhtml(str) {
  // Like escapehtml but doesn't escape & since we don't want to have
  // to decode and then reencode.
  return str.replace(/[<>"]/g, (chr) => replacements[chr]);
}

// A registry name should end in a TLD.  We sort TLDs so that the RegExp greedily matches
// the longest TLD: e.g. .community should come before .com.
const REG_NAME = String.raw`(?:[a-z0-9\-_~!$&'()*+,;=%]+[.])+(?:${ tlds.sort(descLength).join('|') })[.]?`;

// TODO: range restrict
// eslint-disable-next-line id-match
const IPV4 = String.raw`\d+(?:\.\d+){3}(?=[/?#]|:\d)`;

// TODO: count hex digits properly.
// eslint-disable-next-line id-match
const IPV6 = String.raw`\[(?!\])(?:[0-9a-f]+(?:[:][0-9a-f]+)*)?(?:[:][:](?:[0-9a-f]+(?:[:][0-9a-f]+)*))?\]`;

const PROTOCOL_REGEX = String.raw`\b([a-z][a-z0-9+\-.]*):`;

const PROTOCOL_PATTERN = new RegExp(`^${ PROTOCOL_REGEX }`, 'i');

const AUTHORITY_REGEX = String.raw`[^/:?#\s<>!,;]*`;
const AUTHORITY_PATTERN = new RegExp(`^(?:[^:/#?]*:)?//(${ AUTHORITY_REGEX })`);
github draft-js-plugins / draft-js-plugins / draft-js-anchor-plugin / src / utils / urlRegex.js View on Github external
export default (_opts) => {
  const opts = Object.assign({ strict: true }, _opts);
  const protocol = `(?:(?:[a-z]+:)?//)${opts.strict ? '' : '?'}`;
  const auth = '(?:\\S+(?::\\S*)?@)?';
  const ip = ipRegex.v4().source;
  const host = '(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)';
  const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
  const tld = `(?:\\.${opts.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`;
  const port = '(?::\\d{2,5})?';
  const path = '(?:[/?#][^\\s"]*)?';
  const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ip}|${host}${domain}${tld})${port}${path}`;

  return opts.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig');
};
github LessWrong2 / Lesswrong2 / packages / lesswrong / components / async / editor-plugins / image / urlRegex.js View on Github external
export default (_opts) => {
  const opts = Object.assign({ strict: true }, _opts);
  const protocol = `(?:(?:[a-z]+:)?//)${opts.strict ? '' : '?'}`;
  const auth = '(?:\\S+(?::\\S*)?@)?';
  const ip = ipRegex.v4().source;
  const host = '(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)';
  const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
  const tld = `(?:\\.${opts.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`;
  const port = '(?::\\d{2,5})?';
  const path = '(?:[/?#][^\\s"]*)?';
  const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ip}|${host}${domain}${tld})${port}${path}`;

  return opts.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig');
};
github kevva / url-regex / lib / index.js View on Github external
'use strict';
var fs = require('fs');
var path = require('path');
var tlds = require('tlds');

tlds = tlds.sort(function (a, b) {
	return b.length - a.length;
});

fs.writeFileSync(path.join(__dirname, '../tlds.json'), JSON.stringify(tlds));
github kevva / url-regex / index.js View on Github external
module.exports = options => {
	options = {
		strict: true,
		...options
	};

	const protocol = `(?:(?:[a-z]+:)?//)${options.strict ? '' : '?'}`;
	const auth = '(?:\\S+(?::\\S*)?@)?';
	const ip = ipRegex.v4().source;
	const host = '(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)';
	const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
	const tld = `(?:\\.${options.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`;
	const port = '(?::\\d{2,5})?';
	const path = '(?:[/?#][^\\s"]*)?';
	const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ip}|${host}${domain}${tld})${port}${path}`;

	return options.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig');
};

tlds

A list of TLDs.

MIT
Latest version published 1 month ago

Package Health Score

75 / 100
Full package analysis