javascript - 逐行读取文本文件并执行函数?

标签 javascript node.js regex

我有这段代码,它检查像这样的逗号分隔字符串

85aecb80-ac00-40e3-813c-5ad62ee93f42,1813724,client@gmail.com
13vg4f20-fc24-604f-2ccc-1af23taf4421,4255729,developer@gmail.com

如果逗号内的值与正则表达式上指定的值相同,则返回值 false 或 true。

当我尝试编写一个脚本来逐行读取 .txt 文件并使用此正则表达式代码检查其正确或错误时,我的问题就出现了。

function test(str){

  let regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; // email regex

  str = str.split(","); 

  // string should be of length 3 with str[1] number of length 7
  if(str && str.length === 3 && Number(str[1]) && str[1] ){

    let temp = str[0].split("-");

    // check for 85aecb80-ac00-40e3-813c-5ad62ee93f42 separately.
    if(temp && temp.length === 5 &&  /[a-zA-Z\d]{8}/.test(temp[0]) &&  /[a-zA-Z\d]{4}/.test(temp[1]) &&  /[a-zA-Z\d]{4}/.test(temp[2]) &&  /[a-zA-Z\d]{4}/.test(temp[3]) &&  /[a-zA-Z\d]{12}/.test(temp[4])){

      // email regex
      if(regex.test(str[2])){
        return true;

      } 
      else{
        return false;
      }

    }
    else{

      return false
    }
  }
  else{

    return false;
  }
}

无法发布我现在尝试过的代码,因为我没有,甚至不知道如何开始

最佳答案

您可以使用ReadLine模块逐行读取文件。您可以附加测试结果列表,然后在文件完全读取后对它们执行某些操作:

const readline = require('readline');
const fs = require('fs');
const inputFileName = './testfile.txt';

const readInterface = readline.createInterface({
    input: fs.createReadStream(inputFileName),
});

let testResults = [];
readInterface.on('line', line => {
    testResult = test(line);
    console.log(`Test result (line #${testResults.length+1}): `, testResult);
    testResults.push({ input: line, testResult } );
});

// You can do whatever with the test results here.
readInterface.on('close', () => {
    console.log("Test results:", testResults);
});

function test(str){

    let regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; // email regex

    str = str.split(","); 

    // string should be of length 3 with str[1] number of length 7
    if(str && str.length === 3 && Number(str[1]) && str[1] ) {

        let temp = str[0].split("-");

        // check for 85aecb80-ac00-40e3-813c-5ad62ee93f42 separately.
        if(temp && temp.length === 5 &&  /[a-zA-Z\d]{8}/.test(temp[0]) &&  /[a-zA-Z\d]{4}/.test(temp[1]) &&  /[a-zA-Z\d]{4}/.test(temp[2]) &&  /[a-zA-Z\d]{4}/.test(temp[3]) &&  /[a-zA-Z\d]{12}/.test(temp[4])){

            // email regex
            if(regex.test(str[2])) {
                return true;
            } else {
                return false;
            }
        } else { 
            return false
        }
    } else {
        return false;
    }
}

关于javascript - 逐行读取文本文件并执行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58948656/

相关文章:

javascript - 使用 Javascript 从 SQLITE Select 接收数据

node.js - 每次服务器重新启动时散列模式都会更改

python - 简单数学表达式的正则表达式

regex - 从文件名中有选择地提取数字

javascript - 在没有循环依赖的情况下重用服务

javascript - 复选框被选中为 false - jquery

node.js - SocketIO 识别断开连接的用户

javascript - Express GraphicsMagick

javascript - 删除关闭按钮(右上角的 X)- 在使用 dojo 创建的对话框中?

regex - Vim:如何仅将外部命令应用于匹配模式的行