将文本文件输入的信息解析为JSON格式

时间:2016-12-04 18:14:56

标签: javascript regex filereader

我试图用JavaScript解析文本输入文件 我想首先将文件分成多个部分,然后我将通过添加到以下代码片段来填写表单。
我试图找到一种方法将输入分成5个部分;联系信息(姓名,电话,电子邮件),目标,关键技能,工作经历和教育  这就是问题所在。我没有正则表达专家。环顾网络,我无法找到任何轻量级的javaScript库来帮助解决这个问题。找到名称之类的关键字,然后匹配所有字符,直到遇到其他关键字,如电话:,这样才有意义,但我不太清楚如何解决这个问题。

function controller() {

function loadFromFile(event) {
    var fileInput = event.target.files[0];
    var textType = /txt.*/;

    if (fileInput.type.match(textType)) {
        var reader = new FileReader();
        reader.onload = function(evt) {
            console.log(evt.target.result);
        };
        reader.onerror = function(evt) {
            errorLogger('cannot_read_file', 'The file specified cannot be read ');
        };
        reader.readAsText(fileInput);
    } else {}
}
$(':input[type="file"]').change(loadFromFile);
};

Name: John Doe
Phone: (555) 555-5555
Email: johndoe@example.com

OBJECTIVE  Excel in a web developer career.

KEY SKILLS Development: HTML5, JavaScript, Bootstrap, AngularJS, ReactJS, CSS3, Media Queries, 
Development Project Management: JIRA, Bitbucket, Confluence, Git, GitHub

EMPLOYMENT HISTORY 
Title: Junior Web Developer 
Company: Apple Inc.  
Dates: June 2015 to September 2016
* Developed responsive corporate websites
* Did some cool stuff
* Led team in closing out JIRA bugs

Title: Web Development Intern  
Company: Google Inc. 
Dates: January 2015 to May 2015
* Went on coffee runs for the team
* Team record for longest keg stand
* Once ate 82 cupcakes during a team building event

EDUCATION Degree: BBA  
School: Michigan State University 
GPA: 2.2 Major: 
Computer Science Minor: Drinking

1 个答案:

答案 0 :(得分:1)

如果输入始终是完全相同的格式,则此正则表达式有效。

/Name: ([a-zA-Z ]+)\nPhone: (\(\d{3}\) \d{3}-\d{4})\nEmail: (.+@.+)\n{2}OBJECTIVE (.*)\n{2}KEY SKILLS (.*)\n{2}EMPLOYMENT HISTORY ((?:(?:(?:\W+|\s+|.*))*))/g;

https://regex101.com/r/Q5OUFw/2

我使用javascript并不是最好的,但这似乎会返回一个充满匹配的数组。

let m;
let matches =[];

while ((m = regex.exec(str)) !== null)
{
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex)
    {
        regex.lastIndex++;
    }
    m.forEach((match, groupIndex) => { 
    matches.push(match);
    });
}

提供7组比赛。

matches[0] =完全匹配

matches[1] =姓名

matches[2] =电话号码

matches[3] =电子邮件

matches[4] =目标

matches[5] =技能

matches[6] =就业历史