line-counter
Version:
A simple class that allows to count lines while iterating over a string
22 lines (19 loc) • 579 B
JavaScript
var regex = /sed|ipsum/g;
var text = require("fs").readFileSync("example.txt");
var LineCounter = require("../");
var counter = new LineCounter(text);
var match;
while ((match = regex.exec(text)) !== null) {
// Example for .countUpTo()
console.log(
"Found '" + match[0] + "'",
"at index", match.index,
"which is in line ", counter.countUpTo(match.index)
);
// Example for .locate()
console.log(
"Found '" + match[0] + "'",
"at index", match.index,
"which is at location", counter.locate(match.index)
);
}