String Parsing

alexcharleswort
Tera Expert

Hi everybody!

Basic background on what I have going on:

I am working on an inbound email script where the email comes in with a name like 'Jones, Tommy L (Tom)'.

I need to take this, split it rearrange it, put it back together, and then have it fill in a reference variable. I have it about 90% complete. The snag I am running into is making sure that it fills in this reference with 'Tommy Jones' and leaves off the middle initial and the nickname. I eventually worked my way up to what I want   - 'Tommy Jones' - but i accomplished it in a way that I know that will not work for all names coming in:

// sep the term name

                  var sep = email.body.name.lastIndexOf(',');

                    var space = email.body.name.lastIndexOf(' ');

                      var firstname = email.body.name.substring(sep + 2, space - 2);

                        var lastname = email.body.name.substring(0,sep);

                      var emp = ((firstname) + ' ' + (lastname));

                           

                              // Get the Term ref

                              var term = '';

                              var termFromEmail = emp;

                              var termRecord = new GlideRecord('sys_user');

                              if (termRecord.get('name', termFromEmail)) {

                                                              term = termRecord.sys_id;

                              }

cart.setVariable(item, 'emp_name', term); // term emp

Sometime these names might come in as 'Jones, Tommy Lee' or 'Jones, Tommy (Tom). Instead of the first name reading from the space back (which it doesn't seem to be doing properly anyway), i want it to read the name until it gets to a space.

Let me know if you guys have any idea or if it needs to be explained better.

Thanks!

1 ACCEPTED SOLUTION

Hi Alex,



Sorry, even with just the one match, it's returning an array.   Just reference the first element in the returned array (zero-based) instead, like this:



      var dept = email.body.department.match(/(\d{5})/)[0];




I think that should do it.   I used the following to test on jsfiddle.net:



var str = 'Whse: 04592 Flooring'


var dept = str.match(/(\d{5})/);


alert("Dept: " + dept[0]);


alert("Dept is type: " + typeof dept);




See if that works for you.




Thanks,


-Brian


View solution in original post

12 REPLIES 12

Brian Dailey1
Kilo Sage

Hi Alex,



Try this out:



        var arrName = email.body.name.split(',');


        var lastName = arrName[0];


        var firstName = arrName[1].split(' ')[0];




Then just concatenate away...




Thanks,


-Brian





Edit:   This assumes that your name is coming in as "Last, First (everything else)".


I guess i should have further said that last names may be hyphenated or spaced like Smith Jones, Samuel or Smith-Jones, Samuel. Would this still work with the space last name?


Yes, it should work for those cases as-is... since your first split is on a comma (','), it shouldn't touch any spaces or hyphens in the last name (arrName[0]).



As long as it is formatted as "(some last name), (everything else)", this should work.



To test things out (different scenarios), I like to use this site:


        Create a new fiddle - JSFiddle




-Brian


That seemed to only pull the last name with a space in front of it. Was there something in the frirstName variable i should have changed?