nth-match

v1.0.0
Find the nth possible match of a regex
regex nth match

nth-match

Build Status

Install

npm install nth-match

Usage

var nthMatch = require('nth-match');
var source = 'hello world';
var match = nthMatch(source, /\w{2}/, 3);

console.log(match)
// > 'lo'

Why not just use the /g modifier?

You might rewrite the example above as:

var matches = /\w{2}/g;
console.log(matches[3]);

but this will give slightly different results. nth-match allows overlaps in matches:

['he', 'el', 'll', 'lo']

whereas using the /g modifier gives:

['he', 'll', 'wo', 'rl']

License

MIT

npm i nth-match

Metadata

  • MIT
  • Whatever
  • joshwnj
  • released 2/9/2015

Downloads

Maintainers