nth-indexof
v1.0.0
Published
Find the nth position of a pattern in a string
Downloads
19
Maintainers
Readme
nth-indexof
Find the nth position of a pattern in a string.
Similar to str.indexOf(pattern)
except that you can choose how many
occurrences of pattern
you wish to skip before getting the index.
Installation
npm install nth-indexof --save
Usage
const indexOf = require('nth-indexof')
const text = 'foo\nbar\nbaz'
console.log('The index of the 2nd linebreak is:', indexOf(text, '\n', 1)) // => 7
API
index = indexOf(haystack, needle[, skip][, offset])
Returns the index of the needle
in the haystack
ignoring the first
skip
occurrences indicated.
Arguments:
haystack
- A string in which to searchneedle
- The string to search forskip
- Optional number of instances ofneedle
to skip before finding the next index (default:0
)offset
- Optional offset in thehaystack
from where to start the search
Returns -1
if no match is found.
License
MIT