如何为 Node 应用程序编写正则表达式,该应用程序的行为也像具有条件的简单 JS linter
How do I write the Regex for a Node app that acts like a simple JS linter with a condition as well
负责编写一个 Nodejs 应用程序来搜索文件并逐行读取每个文件,如果在任何文件中找到查询,则 return 文件名、找到的查询和行号。我快要完成了,但是最后两位让我感到困惑,其中之一是以下内容的正则表达式:
风格指南
o “let a=1”——没有 space 也没有分号
Ø“常量b = 2;” - 没有 space
所以基本上我需要正则表达式来匹配这个模式
我还必须报告违反了哪条规则
我当前的应用return如下:
文件名,条件和它发生的行号已经只是缺少正则表达式和违反规则的声明
{ 文件路径:'test.js',匹配:地图(1){ 'let a = 1' => [ 1 ] } }
整个 Nodejs 应用的代码是:
const readline = require("readline");
const fs = require("fs");
const SearchFiles = (readStream, filePath, queries) => {
let lineCount = 0;
let matches = new Map();
queries.forEach((query) => matches.set(query, []));
return new Promise((resolve, reject) => {
readStream.on("line", (line) => {
lineCount++;
for (let query of matches.keys()) {
if (searchForTerm(line, query))
matches.set(query, [...matches.get(query), lineCount]);
}
});
readStream.on("close", () =>
resolve({
filePath,
matches,
})
);
});
};
const searchForTerm = (line, query) => line.match(query);
const createLineInterfaces = (filePaths) =>
filePaths.map((filePath) => {
const readStream = readline.createInterface({
input: fs.createReadStream(filePath),
});
return {
filePath,
readStream,
};
});
const filesToSearch = ["test.js", "test2.js"];
const queriesToSearch = ["let a = 1"];
let searchProms = createLineInterfaces(
filesToSearch
).map(({ readStream, filePath }) =>
SearchFiles(readStream, filePath, queriesToSearch)
);
Promise.all(searchProms).then((searchResults) =>
searchResults.forEach((result) => console.log(result))
);
文件内容为:
test.js => 让 a = 1 // 在第 1 行
test2.js => 让 b = 8; //第3行
运行上面两个的app returns:
{ filePath: 'test.js', matches: Map(1) { 'let a = 1' => [ 1 ] } }
{ filePath: 'test2.js', matches: Map(1) { 'let a = 1' => [] } }
首先,如果您希望正则表达式匹配具有相邻非 space 字符的等号,或末尾没有分号的行:
\S=|=\S|[^;]$
例如
const rx = /\S=|=\S|[^;]$/;
rx.test("const b =2;"); // true
rx.test("const b = 2"); // true
rx.test("const b = 2;"); // false
负责编写一个 Nodejs 应用程序来搜索文件并逐行读取每个文件,如果在任何文件中找到查询,则 return 文件名、找到的查询和行号。我快要完成了,但是最后两位让我感到困惑,其中之一是以下内容的正则表达式:
风格指南
o “let a=1”——没有 space 也没有分号
Ø“常量b = 2;” - 没有 space
所以基本上我需要正则表达式来匹配这个模式 我还必须报告违反了哪条规则
我当前的应用return如下: 文件名,条件和它发生的行号已经只是缺少正则表达式和违反规则的声明 { 文件路径:'test.js',匹配:地图(1){ 'let a = 1' => [ 1 ] } }
整个 Nodejs 应用的代码是:
const readline = require("readline");
const fs = require("fs");
const SearchFiles = (readStream, filePath, queries) => {
let lineCount = 0;
let matches = new Map();
queries.forEach((query) => matches.set(query, []));
return new Promise((resolve, reject) => {
readStream.on("line", (line) => {
lineCount++;
for (let query of matches.keys()) {
if (searchForTerm(line, query))
matches.set(query, [...matches.get(query), lineCount]);
}
});
readStream.on("close", () =>
resolve({
filePath,
matches,
})
);
});
};
const searchForTerm = (line, query) => line.match(query);
const createLineInterfaces = (filePaths) =>
filePaths.map((filePath) => {
const readStream = readline.createInterface({
input: fs.createReadStream(filePath),
});
return {
filePath,
readStream,
};
});
const filesToSearch = ["test.js", "test2.js"];
const queriesToSearch = ["let a = 1"];
let searchProms = createLineInterfaces(
filesToSearch
).map(({ readStream, filePath }) =>
SearchFiles(readStream, filePath, queriesToSearch)
);
Promise.all(searchProms).then((searchResults) =>
searchResults.forEach((result) => console.log(result))
);
文件内容为:
test.js => 让 a = 1 // 在第 1 行
test2.js => 让 b = 8; //第3行
运行上面两个的app returns:
{ filePath: 'test.js', matches: Map(1) { 'let a = 1' => [ 1 ] } }
{ filePath: 'test2.js', matches: Map(1) { 'let a = 1' => [] } }
首先,如果您希望正则表达式匹配具有相邻非 space 字符的等号,或末尾没有分号的行:
\S=|=\S|[^;]$
例如
const rx = /\S=|=\S|[^;]$/;
rx.test("const b =2;"); // true
rx.test("const b = 2"); // true
rx.test("const b = 2;"); // false