从文本正文中获取字符串的行号
Get the line number of a string from body of text
假设我有一个包含两行的文件,我在文件中得到 indexOf
一个子字符串。它 returns 和 18
子字符串所在的字符。我怎样才能找到包含此信息的行?
var file = [
'var foo = "hello"',
'console.log(foo)',
].join('\n')
var char = file.indexOf('console') // => 18
var line = lineOfChar(file, char) // => 2
实现此目的的一种可能方法是像您所做的那样查找字符串:
var index = file.indexOf('console'); // => 18
然后使用该索引创建一个包含该索引之前所有内容的子字符串:
var tempString = str.substring(0, index);
最后我们计算 \n
:
的出现次数
var lineNumber = tempString.split('\n').length;
// You should do - 1 if you want your 'first' line to be 0
var assert = require('assert')
var file = [
'var alpha = "hello"',
'var beta = "hello"',
'var gamma = "hello"',
'var delta = "hello"',
'var episilon = "hello"'
].join('\n')
function getLine (body, charOrString) {
if (!body) return false
if (!charOrString) return false
var char = (typeof charOrString === 'string') ? body.indexOf(charOrString) : charOrString
var subBody = body.substring(0, char)
if (subBody === '') return false
var match = subBody.match(/\n/gi)
if (match) return match.length + 1
return 1
}
assert.equal(getLine(file, 'missing'), false)
assert.equal(getLine(file, 'alpha'), 1)
assert.equal(getLine(file, 'beta'), 2)
assert.equal(getLine(file, 'gamma'), 3)
assert.equal(getLine(file, 'delta'), 4)
assert.equal(getLine(file, 'episilon'), 5)
function lineOf(text, substring){
var line = 0, matchedChars = 0;
for (var i = 0; i < text.length; i++) {
text[i] === substring[matchedChars] ? matchedChars++ : matchedChars = 0;
if (matchedChars === substring.length){
return line;
}
if (text[i] === '\n'){
line++;
}
}
return -1;
}
避免遍历字符串两次,一次是查找子字符串,另一次是查找换行符。
假设我有一个包含两行的文件,我在文件中得到 indexOf
一个子字符串。它 returns 和 18
子字符串所在的字符。我怎样才能找到包含此信息的行?
var file = [
'var foo = "hello"',
'console.log(foo)',
].join('\n')
var char = file.indexOf('console') // => 18
var line = lineOfChar(file, char) // => 2
实现此目的的一种可能方法是像您所做的那样查找字符串:
var index = file.indexOf('console'); // => 18
然后使用该索引创建一个包含该索引之前所有内容的子字符串:
var tempString = str.substring(0, index);
最后我们计算 \n
:
var lineNumber = tempString.split('\n').length;
// You should do - 1 if you want your 'first' line to be 0
var assert = require('assert')
var file = [
'var alpha = "hello"',
'var beta = "hello"',
'var gamma = "hello"',
'var delta = "hello"',
'var episilon = "hello"'
].join('\n')
function getLine (body, charOrString) {
if (!body) return false
if (!charOrString) return false
var char = (typeof charOrString === 'string') ? body.indexOf(charOrString) : charOrString
var subBody = body.substring(0, char)
if (subBody === '') return false
var match = subBody.match(/\n/gi)
if (match) return match.length + 1
return 1
}
assert.equal(getLine(file, 'missing'), false)
assert.equal(getLine(file, 'alpha'), 1)
assert.equal(getLine(file, 'beta'), 2)
assert.equal(getLine(file, 'gamma'), 3)
assert.equal(getLine(file, 'delta'), 4)
assert.equal(getLine(file, 'episilon'), 5)
function lineOf(text, substring){
var line = 0, matchedChars = 0;
for (var i = 0; i < text.length; i++) {
text[i] === substring[matchedChars] ? matchedChars++ : matchedChars = 0;
if (matchedChars === substring.length){
return line;
}
if (text[i] === '\n'){
line++;
}
}
return -1;
}
避免遍历字符串两次,一次是查找子字符串,另一次是查找换行符。