url vs regex

Benchmark created on


Setup

const whitelistedUrls = [
    'http://wallapop.com/item/lamp-123',
    'https://wallapop.com/item/lamp-123',
    'https://beta.wallapop.com/item/lamp-123',
    'https://wallapop.com/user/john-123',
    'https://beta.wallapop.com/user/john-123',
    'https://wallapop.com/app/user/john-123',
    'https://beta.wallapop.com/app/user/john-123',
    'https://p.wallapop.com/p/123',
    'wallapop.com',
    'www.wallapop.com',
    'fr.wallapop.com',
    'es.wallapop.com',
    'it.wallapop.com',
    'pt.wallapop.com',
    'uk.wallapop.com',
    'https://www.wallapop.com/item/lamp-123',
    'https://www.wallapop.com',
    'https://es.wallapop.com',
  ];

Test runner

Ready to run.

Testing in
TestOps/sec
regex
const ALLOWED_SUBDOMAIN = '(?:(?:www|fr|it|pt|es|uk)\\.)?';
const PROTOCOL = '(?:https?:\\/\\/)?';
const BASE_URL = `${PROTOCOL}${ALLOWED_SUBDOMAIN}(?:beta\\.)?wallapop\\.com`;

const BASE_PATTERN = new RegExp(`^${BASE_URL}(?:\\/.*)?$`);
const ITEM_PATTERN = new RegExp(`^${BASE_URL}\\/item\\/.+`);
const USER_PATTERN_FOR_WEB = new RegExp(`^${BASE_URL}\\/user\\/.+`);
const USER_PATTERN = new RegExp(`^${BASE_URL}\\/app\\/user\\/.+`);
const P_PATTERN = /^(?:https?:\/\/)?p\.wallapop\.com\/p\/.+/;

const WHITELISTED_TEXT_MESSAGE_LINK_REGEX = new RegExp(
  `(${BASE_PATTERN.source}|${ITEM_PATTERN.source}|${USER_PATTERN_FOR_WEB.source}|${USER_PATTERN.source}|${P_PATTERN.source})`,
);

const isWhitelistedTextMessageLink = (text) => {
  return WHITELISTED_TEXT_MESSAGE_LINK_REGEX.test(text);
};

for (const url of whitelistedUrls) {
	console.log('url', url, isWhitelistedTextMessageLink(url));
}
ready
url
const ALLOWED_SUBDOMAINS = ['www', 'fr', 'it', 'pt', 'es', 'uk'];
const isWhitelistedTextMessageLink = (text) => {
  try {
    // Add protocol if missing
    const urlString = /^https?:\/\//.test(text) ? text : `https://${text}`;
    const url = new URL(urlString);
    
    // Check p.wallapop.com
    if (url.hostname === 'p.wallapop.com') {
      return url.pathname.startsWith('/p/');
    }
    
    // Must end with wallapop.com
    if (!url.hostname.endsWith('wallapop.com')) {
      return false;
    }
    
    // Extract prefix before 'wallapop.com'
    const prefix = url.hostname.replace(/\.?wallapop\.com$/, '');
    
    // No subdomain is valid
    if (!prefix) {
      return true;
    }
    
    // Split subdomains (e.g., 'www.beta' -> ['www', 'beta'])
    const parts = prefix.split('.');
    
    // Check each part: must be either 'beta' or an allowed subdomain
    for (const part of parts) {
      if (part !== 'beta' && !ALLOWED_SUBDOMAINS.includes(part)) {
        return false;
      }
    }
    
    // Valid combinations: www.beta, beta, www, fr, it, etc.
    return true;
    
  } catch {
    return false;
  }
};
for (const url of whitelistedUrls) {
  console.log('url', url, isWhitelistedTextMessageLink(url));
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.