解析从文本文件输入到JSON格式的信息
问题描述:
我试图解析JavaScript中的文本输入文件。
我想先将文件分成几个部分,然后通过添加下面的代码片段来填充表单。
我试图找到一种方法来将输入分成5个部分;联系信息(姓名,电话,电子邮件),目标,关键技能,就业历史和教育。
而这里存在的问题。我不是正则表达式专家。环顾网络,我无法找到任何轻量级的javaScript库来帮助解决这个问题。这是有意义的寻找关键字,如名称:,然后匹配所有字符,直到遇到另一个关键字,如电话:但我不知道如何处理这个问题。解析从文本文件输入到JSON格式的信息
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: [email protected]
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
答
此正则表达式的作品,所提供的输入始终是完全相同的格式。
/Name: ([a-zA-Z ]+)\nPhone: (\(\d{3}\) \d{3}-\d{4})\nEmail: ([email protected]+)\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]
=工作经历