The correct mention(s) regex. Regular expression for twitter, facebook, github, etc user mentions
$ npm install mentions-regex
$ npm test
Default regex is
\s+@(\w{1,30})\s+
[options]
{Object}startSpace
{Boolean} iffalse
, will remove starting\s+
from regexendSpace
{Boolean} iffalse
, will remove ending\s+
from regexlength
{Number} maximum length of mention, default30
match
{String} what to match, default is\w{1,30}
flags
{String} every valid RegExp flag, defaultundefined
dot
{Boolean} will use[A-Za-z0-9_.]
instead of\w
return
{RegExp}
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']
Charlike Mike Reagent
Copyright (c) 2014 Charlike Mike Reagent, contributors.
Released under the MIT
license.
Powered and automated by readdirp + hogan.js, December 21, 2014