Reputation: 195
I'm attempting to parse a text input file in JavaScript.
I want to first break the file into sections which I will then use to fill in a form by adding to the following code snippet.
I'm trying to find a way to to break the input into 5 sections; contact info (name, phone, email), objective, key skills, employment history, and education.
And here in lies the problem. I'm no regex expert. Looking around the web I couldn't find any lightweight javaScript libraries to help out with this. It would make sense to look for keywords such as name:, then match all characters until another keyword is encountered, like phone: but I don't quite know how to approach this problem.
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
Upvotes: 0
Views: 151
Reputation: 178
This regex works, provided the input is always the same exact format.
/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
I'm not the best with javascript, but this seems to return an array full of matches.
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);
});
}
Provides 7 group matches.
matches[0]
= Full Match
matches[1]
= Name
matches[2]
= Phone Number
matches[3]
= Email
matches[4]
= Objective
matches[5]
= Skills
matches[6]
= Employment history
Upvotes: 1