get-line-from-pos

2019-10-10 admin

get-line-from-pos是什么

什么是get-line-from-pos,Get the line number of a string using index or position

get-line-from-pos使用教程帮助文档

get-line-from-pos

Get the line number of a string using index or position

NPM Version NPM Downloads Build Status

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 = require('get-line-from-pos');

var str = 'string with \n multiple \n lines';
console.log(getLineFromPos(str, /multiple/g.exec(str)));
// -> 2

AMD

define(['getLineFromPos'], function(getLineFromPos){
    var pos = getLineFromPos(multipleLineStr, index);
    console.log(pos);
    //-> line number that index is on
});

Browser

Load the script:

<script src="../get-line-from-pos/index.js"></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

MIT ©Gilad Peleg

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-77050.html

文章标题:get-line-from-pos

回到顶部