Skip to content

100% twitter compatible `@mentions` regex! Regular expression for matching `@username` mentions, as used on twitter, facebook, github, etc.

License

Notifications You must be signed in to change notification settings

regexhq/mentions-regex

Repository files navigation

npm versi mit license build status deps status regexps org

The correct mention(s) regex. Regular expression for twitter, facebook, github, etc user mentions

Install

$ npm install mentions-regex
$ npm test

Default regex is \s+@(\w{1,30})\s+

  • [options] {Object}
    • startSpace {Boolean} if false, will remove starting \s+ from regex
    • endSpace {Boolean} if false, will remove ending \s+ from regex
    • length {Number} maximum length of mention, default 30
    • match {String} what to match, default is \w{1,30}
    • flags {String} every valid RegExp flag, default undefined
    • dot {Boolean} will use [A-Za-z0-9_.] instead of \w
  • return {RegExp}

Usage

For more use-cases see tests

var mentionsRegex = require('mentions-regex');

mentionsRegex().test('github @tunnckoCore')
//=> false

mentionsRegex({flags: 'g'}).test('github @tunnckoCore')
//=> false

mentionsRegex({endSpace: false}).test('github @tunnckoCore')
//=> true

var str = '@first git @tunnckoCore and @face some @al.so [email protected] glob @last'

str.match(mentionsRegex())
//=> [' @tunnckoCore ']

str.match(mentionsRegex({flags: 'g'}))
//=> [' @tunnckoCore ', ' @face ']

str.match(mentionsRegex({flags: 'g', startSpace: false}))
//=> ['@first ', '@tunnckoCore ', '@face ']

str.match(mentionsRegex({flags: 'g', endSpace: false}))
//=> [' @tunnckoCore ', ' @face ', ' @al', ' @last']

str.match(mentionsRegex({flags: 'g', startSpace: false, endSpace: false}))
//=> ['@first', '@tunnckoCore', '@face', '@al', '@here', '@last']

str.match(mentionsRegex({length: 5}))
//=> [' @face ']

str.match(mentionsRegex({flags: 'g', dot: true}))
//=> [' @tunnckoCore ', ' @face ', ' @al.so ']

str.match(mentionsRegex({flags: 'g', dot: true, length: 5}))
//=> [' @face ', ' @al.so ']

str.match(mentionsRegex({flags: 'g', dot: true, startSpace: false}))
//=> ['@first ', '@tunnckoCore ', '@face ', '@al.so ', '@here.com ']

str.match(mentionsRegex({flags: 'g', dot: true, startSpace: false, endSpace: false}))
//=> ['@first', '@tunnckoCore', '@face', '@al.so', '@here.com ', '@last']

Author

Charlike Mike Reagent

License MIT license

Copyright (c) 2014 Charlike Mike Reagent, contributors.
Released under the MIT license.


Powered and automated by readdirp + hogan.js, December 21, 2014

About

100% twitter compatible `@mentions` regex! Regular expression for matching `@username` mentions, as used on twitter, facebook, github, etc.

Resources

License

Stars

Watchers

Forks

Packages

No packages published