0

enter image description here

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 based on Efficiently parsing email body in javascript:

function parse (i, body) {
 var obj = {}; 
 body.split('\n').forEach(v=>v.replace(/\s*(.*)\s*:\s*(.*)\s*/, (s,key,val)=>{obj[key]=isNaN(val)||val.length<1?val||undefined:Number(val);}));

 var objArr = Object.values(obj);

 var res = [];
 res[0] = i
 res.push(objArr)

 return res

}

when I run this I get a syntax error in:

 body.split('\n').forEach(v=>v.replace(/\s*(.*)\s*:\s*(.*)\s*/, (s,key,val)=>{obj[key]=isNaN(val)||val.length<1?val||undefined:Number(val);}));

what am I doing wrong ?

7
  • What's the exact error? Also, you haven't done yourself any favors by sticking everything on one line. Line numbers would have be able to help you narrow it down. I'm going to guess it's a misplaced brace problem. Commented Aug 26, 2017 at 19:37
  • In the apps script editor it just says "Syntax error. (line 43, file "Code") (that line). I ran the code in jslint and the output said "Wrap the parameter in parens." Commented Aug 26, 2017 at 19:45
  • Trying splitting it over a few lines so you can get a better location. Also, running this is a debugger would probably work as well. Commented Aug 26, 2017 at 19:46
  • Cannot reproduce. parse(0, "Name: Bob smith\nEmail: [email protected]\nPhone Number: 4243331212") gives back an array of "0,Bob smith,[email protected],4243331212". Not sure what that prefixed "0" is, but I didn't get any errors. Tested in the Edge console. Commented Aug 26, 2017 at 20:00
  • yes that's what it should so. The code from the previous question did work. I'm trying to run it as apps script and that's where the problem occurs. I've added a screenshot. The error is occuring on line 44 Commented Aug 26, 2017 at 20:22

1 Answer 1

1

Google Apps Script is based on the ECMA Script version that doesn't support arrow functions. Replace

array.forEach(element => element.replace(expression))

with

array.forEach(function(element) {

return element.replace(expression);

});
Sign up to request clarification or add additional context in comments.

2 Comments

I;ve changed it to : body.split('\n').forEach(function(v) { return v.replace(/\s*(.*)\s*:\s*(.*)\s*/, (s,key,val)=>{obj[key]=isNaN(val)||val.length<1?val||undefined:Number(val);})}) - However I'm still getting a syntax error. does ' (s,key,val)=>...' also need to be replaced?
Everything with => in it must be replaced with proper functions (with parameters and return statement). For example function (a, b, c) { return a + b + c; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.