DEV Community

Matt Kenefick
Matt Kenefick

Posted on

Regex: Parsing usernames from Twitter, Facebook, and Instagram

For a few of my recent projects, we've had to parse out usernames from various social media URLs. We usually do this on user settings pages to make it easy when filling out forms; you can either write in your handle or paste in a URL.

In PHP

<?php

$urls = [
    'https://www.twitter.com/mattkenefick',
    'http://www.twitter.com/mattkenefick',
    'www.twitter.com/mattkenefick',
    'twitter.com/mattkenefick',
    'https://www.instagram.com/tonightaliveofficial',
    'http://www.instagram.com/tonightaliveofficial',
    'www.instagram.com/tonightaliveofficial',
    'instagram.com/tonightaliveofficial',
    'vimeo.com/my-name-goes-here',
    'medium.com/@mattkenefick',
    'basic-name',
    '12345678',
    '',
];

/**
 * Parse username from social media URL
 *
 * @param string $url
 * @return string
 */
function parseUsername(string $url): string
{
    $output = $url;

    // Parse username
    preg_match('/(?:https?:\/\/)?(?:www.)?(?:twitter|medium|facebook|vimeo|instagram)(?:.com\/)?([@a-zA-Z0-9-_]+)/im', $url, $matches);

    // Set output
    $output = count($matches) ? $matches[1] : $output;

    return $output;
}

// Parse
foreach ($urls as $url) {
    $name = parseUsername($url);

    echo "Extacted: [$name] from $url \n";
}

// Extacted: [mattkenefick] from https://www.twitter.com/mattkenefick
// Extacted: [mattkenefick] from http://www.twitter.com/mattkenefick
// Extacted: [mattkenefick] from www.twitter.com/mattkenefick
// Extacted: [mattkenefick] from twitter.com/mattkenefick
// Extacted: [tonightaliveofficial] from https://www.instagram.com/tonightaliveofficial
// Extacted: [tonightaliveofficial] from http://www.instagram.com/tonightaliveofficial
// Extacted: [tonightaliveofficial] from www.instagram.com/tonightaliveofficial
// Extacted: [tonightaliveofficial] from instagram.com/tonightaliveofficial
// Extacted: [my-name-goes-here] from vimeo.com/my-name-goes-here
// Extacted: [@mattkenefick] from medium.com/@mattkenefick
// Extacted: [basic-name] from basic-name
// Extacted: [12345678] from 12345678
// Extacted: [] from
Enter fullscreen mode Exit fullscreen mode

In JavaScript

// One-liner
(url.match(/(?:https?:\/\/)?(?:www.)?(?:twitter|medium|facebook|vimeo|instagram)(?:.com\/)?([@a-zA-Z0-9-_]+)/im) || [url])[1];

// Function
function parseUsername(url)
{
    let output = url;
    let matches;

    // Parse username
    matches = url.match(/(?:https?:\/\/)?(?:www.)?(?:twitter|medium|facebook|vimeo|instagram)(?:.com\/)?([@a-zA-Z0-9-_]+)/im);

    // Set output
    output = matches.length ? matches[1] : output;

    return output;
}
Enter fullscreen mode Exit fullscreen mode

Here's a testable example. in JavaScript. https://jsfiddle.net/124ojfmp/

Discussion (0)