Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 逐行读取文本文件并执行函数?_Javascript_Node.js_Regex - Fatal编程技术网

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

Javascript 逐行读取文本文件并执行函数?,javascript,node.js,regex,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 te

我有一段代码,它检查逗号分隔的字符串,如下所示

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模块逐行读取文件。您可以附加一个测试结果列表,然后在文件完全读取后对其进行处理:

var fs = require('fs');
const readline = require('readline');
const readInterface = readline.createInterface({
input: fs.createReadStream('read.txt'),
output: process.stdout,
console: false
});

readInterface.on('line', function (line) {
  console.log(line);
  test(line);
});
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;
}
}
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;
    }
}

您可以使用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;
    }
}

我将所有正则表达式组合成一个正则表达式:

/[a-zA-Z\d]{8}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{12},\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/gm

用这个正则表达式来测试你的句子。不要拆分字符串,而是将其视为一个整体

我将所有正则表达式组合成一个正则表达式:

/[a-zA-Z\d]{8}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{12},\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/gm
用这个正则表达式来测试你的句子。不要拆分字符串,而是将其视为一个整体