Forem

Matt Kenefick
Matt Kenefick

Posted on

3 1

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/

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay