简体   繁体   English

从文本文件读取Javascript

[英]Reading From text file Javascript

Okay, this may be hard to explain. 好的,这可能很难解释。
The Passwords don't work the usernames do. 密码对用户名无效。
I am reading from a text file. 我正在从一个文本文件中读取。

"username,password" is the structure for the below is the text file “用户名,密码”是下面的结构,是文本文件

John,BOL12345
Mary2,BOL77777
Anna,BOL54321
test,BOL12345

The top 3 do not work alone i only need the top 3 前三名并不孤单,我只需要前三名
but once i add the "test,BOL12345" 但是一旦我添加了“测试,BOL12345”
the password BOL12345 does work 密码BOL12345可以正常工作
but without the "test,BOL12345" The password "BOL12345" does not work or any of the other ones 但没有“ test,BOL12345”密码“ BOL12345”或其他任何密码均无效
I am doing this all in javascript below will be the code snippet.. please ask any questions as i do not understand why this happens. 我正在下面的javascript中执行所有操作,这将是代码段..请问任何问题,因为我不明白为什么会发生这种情况。

The JavaScript Below 下面的JavaScript
The "lines" = the text file above “行” =上面的文本文件

    lines = x.responseText.split("\n");
    for (i=0; i < lines.length; i++)
    {
        test1 = lines[i].split(",")
        username.push(test1[0]);
        password.push(test1[1]);

    }
    var tempUsername = document.getElementById('username').value;
    var tempPassword = document.getElementById('password').value;
    var arraycontainsusername = (username.indexOf(tempUsername) > -1);
    var arraycontainspassword = (password.indexOf(tempPassword) > -1);
    alert(password);
    if (arraycontainsusername && arraycontainspassword) {
        window.location.href = "listing.htm";
    };

Educated guess: your file is using \\r\\n . 有根据的猜测:您的文件正在使用\\r\\n since you're splitting by \\n the \\r is left in and corrupts each string. 因为您要用\\n分割,所以\\r被保留并破坏了每个字符串。 try splitting by \\r\\n and see what happens. 尝试按\\r\\n分割,看看会发生什么。 That would explain why adding the last line would work, since there's no newline at the end there won't be a trailing character to mess up the indexOf search. 这可以解释为什么添加最后一行会起作用,因为末尾没有换行符,不会有结尾字符来弄乱indexOf搜索。

different operating systems handle text files differently. 不同的操作系统对文本文件的处理方式也不同。 Windows uses CRLF (Carriage Return Line Feed) to jump to the next line, while *NIX variants use LF. Windows使用CRLF(回车换行)跳到下一行,而* NIX变体使用LF。 old MacOS versions use CR. 旧的MacOS版本使用CR。 Your code was assuming the file came from a *NIX environment, where LF (or \\n ) is the norm, when it came from a windows environment, where CRLF (or \\r\\n ) is the norm (not accurate since you can make text files with LF in windows and with CRLF in *NIX, buy you get the picture). 您的代码假设文件来自* NIX环境,其中LF(或\\n )是标准,而来自Windows环境,文件中的CRLF(或\\r\\n )是标准(不准确,因为您可以使用Windows中的LF和* NIX中的CRLF制作文本文件,请买得到图片)。

To handle all cases correctly, I'd recommend normalizing the string before working on it: 为了正确处理所有情况,建议您在处理字符串之前先对其进行规范化:

x.responseText.replace(/\\r\\n|\\r(?!\\n)/g, '\\n').split('\\n');

that seemingly chinese string in the middle is actually a regular expression that matches either \\r\\n or \\r (but only when \\r isn't followed by \\n). 在中间的看似中文字符串实际上是一个匹配\\ r \\ n或\\ r的正则表达式(但仅当\\ r不跟着\\ n时才匹配)。 this way you can replace all your CRLFs and CRs to LF and handle text coming from any environment. 这样,您可以将所有CRLF和CR替换为LF,并处理来自任何环境的文本。

you can simplify that regex because of the order of the tokens, to /\\r\\n|\\r/ , but I'm leaving it in because it illustrates a neat concept (lookaheads - that bit (?!\\n) says if and only if not immediately followed by a \\n ). 您可以根据令牌的顺序将该正则表达式简化为/\\r\\n|\\r/ ,但是我将其保留下来是因为它说明了一个简洁的概念(先行–该位(?!\\n)表示如果并且仅当未紧跟\\n时才可以。 With that said /\\r\\n|\\r/ will perform better, especially when handling large files 这样, /\\r\\n|\\r/会更好,尤其是在处理大文件时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM