get-line-from-pos
Get the line number of a string using index or position
Lookup a line number in a string using an index or position.
Useful if you want to get a line number of a position or index from a regex match, but can also be used in other situations.
- Allows using negative positions to lookup line number.
- Supported by CommonJS, AMD and browser.
- Supports Regular line ending style (
\n
) and MacOS line ending style (\r
).
Installation
$ npm install --save get-line-from-pos
Usage
CommonJS
var getLineFromPos = ; var str = 'string with \n multiple \n lines';console;// -> 2
AMD
;
Browser
Load the script:
Then when you need to use it:
console.log(getLineFromPos(someStr, -1));
//-> Will output the number of lines in someStr
API
getLineFromPos(inputStr, index)
Both parameters are required.
inputStr
Type: String
Required
String to do the lookup for line number.
index
Type: Number
Required
The index or position to lookup the line number.
License
Copyright © 2014 Gilad Peleg. Licensed under the MIT license.