最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to extract instagram username from string - Stack Overflow

programmeradmin1浏览0评论

I have an input field and my users are entering their instagram username in various formats

@username

/
instagram/username

how can I extract username only?

with

(?:(?:http|https):\/\/)?(?:www.)?(?:instagram|instagr.am)\/([A-Za-z0-9-_]+)

I can extract from the URL. not sure how to search for whatever is after @

I have an input field and my users are entering their instagram username in various formats

@username
https://www.instagram./username
https://www.instagram./username/
instagram./username

how can I extract username only?

with

(?:(?:http|https):\/\/)?(?:www.)?(?:instagram.|instagr.am)\/([A-Za-z0-9-_]+)

I can extract from the URL. not sure how to search for whatever is after @

Share Improve this question edited May 19, 2020 at 4:29 handsome asked May 19, 2020 at 4:21 handsomehandsome 2,43211 gold badges54 silver badges81 bronze badges 1
  • 1 Try using a regex that looks for a prefix of @ or the website URL. If you're still having trouble with that, e back here and show what you tried – Phil Commented May 19, 2020 at 4:24
Add a ment  | 

3 Answers 3

Reset to default 9

You want a regex that matches either @ or various forms of the URL version as a prefix to the username, followed by an optional forward-slash.

Something like this

/^(?:@|(?:https?:\/\/)?(?:www\.)?instagr(?:\.am|am\.)\/)?(\w+)\/?$/

Breaking it down

^
(?:
  @                     - literal "@"
  |                     - or
  (?:https?:\/\/)?      - optional HTTP / HTTPS scheme
  (?:www\.)?            - optional "www."
  instagr(?:\.am|\.) - "instagram." or "instgr.am"
  \/                    - forward-slash
)?                      - the whole prefix is optional
(\w+)                   - capture group for the username. Letters, numbers and underscores
\/?                     - optional trailing slash
$

const inputs = [
  '@username', 
  'https://www.instagram./username', 
  'https://www.instagram./username/', 
  'instagram./username', 
  'handsome_jack',
  'http://example./handsome'
]

const rx = /^(?:@|(?:https?:\/\/)?(?:www\.)?instagr(?:\.am|am\.)\/)?(\w+)\/?$/

inputs.forEach(input => {
  let match = rx.exec(input) 
  if (match) {
    console.log(input, match[1]) 
  }
})

Building on top @Phil's answer. It ignores instagram usernames which has a period "." in them.

/^(?:@|(?:https?:\/\/)?(?:www\.)?instagr(?:\.am|am\.)\/)?([\w\.]+)\/?$/g

This should take care of the insta usernames with period as well.

I have simplified Phil's solution into a function - all credits go to him.

Function:

function getInstagramUsernames(list) {

    const _regex = /^(?:@|(?:https?:\/\/)?(?:www\.)?instagr(?:\.am|am\.)\/)?(\w+)\/?$/;

    let results = [];

    for (const each of list) {
        let match = _regex.exec(each)
        if (match) {
            results.push(match[1])
            console.log(match[1]);
        }
    }

    return results;
}

Usage:

var list = [
    '@username',
    'https://www.instagram./usesda',
    'https://www.instagram./username11/',
    'instagram./username',
    'handsome_jack',
    'http://example./handsome'
]
getInstagramUsernames(list); // ['username', 'usesda', 'username11', 'username', 'handsome_jack']
发布评论

评论列表(0)

  1. 暂无评论