I need to parse multiple email bodies that look like:
Name: Bob smith
Email: [email protected]
Phone Number: 4243331212
As part of a larger program I have the following function to parse the page:
function parseBody(i, body) {
result = []
result[0] = i
var name = body.match(new RegExp(/\*Name:\*(.*) /))
if (name) {
result[1] = name[1]
}
......
return result;
}
rather than have potentially 10 fields to parse and load into the result array by if statements , is there a more efficient way using JavaScript to parse this page and load the array?
I need to parse multiple email bodies that look like:
Name: Bob smith
Email: [email protected]
Phone Number: 4243331212
As part of a larger program I have the following function to parse the page:
function parseBody(i, body) {
result = []
result[0] = i
var name = body.match(new RegExp(/\*Name:\*(.*) /))
if (name) {
result[1] = name[1]
}
......
return result;
}
rather than have potentially 10 fields to parse and load into the result array by if statements , is there a more efficient way using JavaScript to parse this page and load the array?
Share Improve this question asked Aug 26, 2017 at 15:30 user1592380user1592380 36.6k105 gold badges314 silver badges553 bronze badges 2- I don't see what you mean by "10 fields"? The example you gave has only 3. – Bergi Commented Aug 26, 2017 at 15:57
- Just split into lines and take the part after the colon on every one? – Bergi Commented Aug 26, 2017 at 15:58
2 Answers
Reset to default 3handles numbers and undefined
var mailBody = `
Name: Bob smith
Email: [email protected]
Phone Number: 4243331212
key4:value4
key5:value 5
key6:
key7: value7
`;
var obj = {};
mailBody.split('\n').forEach(v=>v.replace(/\s*(.*)\s*:\s*(.*)\s*/, (s,key,val)=>{
obj[key]=isNaN(val)||val.length<1?val||undefined:Number(val);
}));
console.log( obj );
I would suggest to find a way to split the body and identify the keys and values to build a result object.
In order to split the body, you can use a regular expression to match the structure of a key:
let delimiter = new RegExp('(\w*): ')
Then use the split method on the body with this regexp to get an array with an alternance of keys and values :
let split = body.split(delimiter)
Finally sort the keys from the values with a loop :
let res = {} for(let i = 0; i < split.length; i += 2) res[ split[i] ] = split[ i+1 ] // even indexes are key, odd ones are values
Pushing forward you can remove empty keys and trailing spaces and carriage return with a more advanced regexp. Here is a possible implementation :
function parseBody (body) {
let split = body
.split(new RegExp('(\w*): ([^\t\r\n]*)[\t\r\n]*')) // removes trailing line feed
.filter(x=>x) // remove empty matches
let res = {}
for(let i = 0; i < split.length; i += 2)
res[ split[i] ] = split[ i+1 ] // even indexes are key, odd ones are values
return res
}
This returns an associative array but you have the idea if you want a simple array.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744895589a4599675.html
评论列表(0条)