Skip to content

Commit 245949f

Browse files
author
Charlike Mike Reagent
committed
add usage
1 parent 5dbc294 commit 245949f

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@
77

88
'use strict';
99

10-
module.exports = function metntionsRegex(opts) {
11-
opts = opts || {};
10+
/**
11+
* The correct mention(s) regex. Regex done right!
12+
*
13+
* @param {Object} `options`
14+
* @return {RegExp}
15+
*/
16+
module.exports = function metntionsRegex(options) {
17+
options = options || {};
1218

13-
var startSpace = opts.startSpace === false ? '' : '(?:\\s+)';
14-
var endSpace = opts.endSpace === false ? '' : '(?:\\s+)';
15-
var length = '{1,' + (opts.length || 30) + '}';
16-
var match = opts.match || '\\w' + length;
19+
var startSpace = options.startSpace === false ? '' : '\\s+';
20+
var endSpace = options.endSpace === false ? '' : '\\s+';
21+
var length = '{1,' + (options.length || 30) + '}';
22+
var match = options.match || '\\w' + length;
1723

18-
match = opts.dot && !opts.match ? '[A-Za-z0-9_.]' + length : match
24+
match = options.dot && !options.match ? '[A-Za-z0-9_.]' + length : match
1925

20-
return new RegExp(startSpace + '@(' + match + ')' + endSpace, opts.flags);
26+
return new RegExp(startSpace + '@(' + match + ')' + endSpace, options.flags);
2127
};

readme.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,62 @@ $ npm install mentions-regex
1010
$ npm test
1111
```
1212

13+
1314
## Usage
1415
> For more use-cases see [tests](./test.js)
1516
17+
```js
18+
var mentionsRegex = require('mentions-regex');
19+
20+
mentionsRegex().test('github @tunnckoCore')
21+
//=> false
22+
23+
mentionsRegex({flags: 'g'}).test('github @tunnckoCore')
24+
//=> false
25+
26+
mentionsRegex({endSpace: false}).test('github @tunnckoCore')
27+
//=> true
28+
29+
var str = '@first github @tunnckoCore and @face some @al.so [email protected] global @last'
30+
31+
str.match(mentionsRegex())
32+
//=> [' @tunnckoCore ']
33+
34+
str.match(mentionsRegex({flags: 'g'}))
35+
//=> [' @tunnckoCore ', ' @face ']
36+
37+
str.match(mentionsRegex({flags: 'g', startSpace: false}))
38+
//=> [' @tunnckoCore ', ' @face ', '@face ']
39+
40+
str.match(mentionsRegex({flags: 'g', endSpace: false}))
41+
//=> [' @tunnckoCore ', ' @face ', ' @al', ' @last']
42+
43+
str.match(mentionsRegex({flags: 'g', startSpace: false, endSpace: false}))
44+
//=> ['@first', '@tunnckoCore', '@face', '@al', '@here', '@last']
45+
46+
str.match(mentionsRegex({length: 5}))
47+
//=> [' @face ']
48+
49+
str.match(mentionsRegex({flags: 'g', dot: true}))
50+
//=> [' @tunnckoCore ', ' @face ', ' @al.so ']
51+
52+
str.match(mentionsRegex({flags: 'g', dot: true, length: 5}))
53+
//=> [' @face ', ' @al.so ']
54+
```
55+
56+
57+
## [.metntionsRegex](index.js#L16)
58+
> Default regex is `\s+@(\w{1,30}|[A-Za-z0-9_.]{1,30})\s+`
59+
60+
* `[options]` **{Object}**
61+
- `startSpace` **{Boolean}** if `false`, will remove starting `\s+` from regex
62+
- `endSpace` **{Boolean}** if `false`, will remove ending `\s+` from regex
63+
- `length` **{Number}** maximum length of mention, default `30`
64+
- `match` **{String}** what to match, default is `\w{1,30}`
65+
- `flags` **{String}** every valid RegExp flag, default `undefined`
66+
- `dot` **{Boolean}** replace options.match which is `\w` with `[A-Za-z0-9_.]`
67+
* `return` **{RegExp}**
68+
1669

1770
## Authors & Contributors
1871
**Charlike Mike Reagent** [![author tips][author-gittip-img]][author-gittip]

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
'use strict';
99

1010
var mentionsRegex = require('./index');
11-
var str = 'some @mention as @he.re a@nd @second ols @He-wee more @men.tion'
12-
13-
console.log(str.match(mentionsRegex({flags: 'g'})))
11+
var str = '@first github @tunnckoCore and @face some @al.so [email protected] global @last'
12+
var res = str.match(mentionsRegex({flags: 'g', dot: true}))
13+
console.log(res)

0 commit comments

Comments
 (0)