Question

Just working on a script to generate a report. Data is form submissions via google form. Emails are collected in form, as well as other data.

ALL email addresses are structured too. Firstname.Lastname@email-domain.co.uk

For the report, I wanted to put in peoples names, not emails.

Here is what I've come up with, and it does work, but I'm new to Javascript and scripts for google. So want to try to get things right, or find out where I'm going wrong. I don't want to write lots of scripts and put them together to find they take too long with too much data and fail.

 var cleanName = names[i].replace("@email-domain.co.uk","");
  var cleanName = cleanName.replace("."," ");
  var cleanName = cleanName.charAt(0).toUpperCase() + cleanName.slice(1);
  var space = cleanName.search(" ");
  var cleanName = cleanName.substr(0,space+1)+ cleanName.charAt(space+1).toUpperCase() + cleanName.slice(space+2);

I tried for about an hour to get it to work without the space var. But just couldn't do it. not that it's the best method.

Was it helpful?

Solution

Updated Answer

It just dawned on me that you could actually trim this down a little further, by using the .join method for arrays:

var namePieces = names[i].replace(/@.+$/, "").split(".");

for (i=0; i<namePieces.length; i++) {
    namePieces[i] = namePieces[i].replace(/^[a-z]/, function(match) {
        return match.toUpperCase();
    });
}

var cleanName = namePieces.join(" ");

. . . or, if you are returning the cleanName value, for that last line, you could just use:

return namePieces.join(" ");

That would actually just convert the first letter of each element in the namePieces array to uppercase and then join all of the updated elements together, with a space separating each element.


Original Answer

Here's an alternate approach . . . based on your code, you could do this:

var namePieces = names[i].replace(/@.+$/, "").split(".");
var cleanName = "";

for (i=0; i<namePieces.length; i++) {
    cleanName += (i > 0) ? " " : "";
    cleanName += namePieces[i].replace(/^[a-z]/, function(match) {
        return match.toUpperCase();
    });
}

Now, let me step through the code . . . :)

var namePieces = names[i].replace(/@.+$/, "").split(".");

This takes the current email address, strips off everything after the @, and splits the remaining values, into an array, every time it hits a .

The result of your sample data Firstname.Lastname@email-domain.co.uk, would be this array: ["Firstname", "Lastname"]

After that, it goes through each item in the array and adds a space to the existing cleanName value, if it is not the first element in the namePieces array:

cleanName += (i > 0) ? " " : "";

Then it takes the current name "piece" and, if it's first character is a lowercase letter, replaces it with the uppercase version of itself, and adds it to the current cleanName value:

cleanName += namePieces[i].replace(/^[a-z]/, function(match) {
    return match.toUpperCase();
});

Two things to note about this approach:

  1. It skips the replace if it not a lowercase letter for a little extra speed. If you need to account for non-alpha characters (other languages, symbols, numbers), this regex would have to be altered to either accept any character (i.e., /^./) or change the set of acceptable characters in the regex.
  2. This approach will also pick up other variations of the names in the email address . . . for example:

    Firstname.M.Lastname@email-domain.co.uk would result in Firstname M Lastname Firstname.Lastname.Jr@email-domain.co.uk would result in Firstname Lastname Jr

    So that might add a little extra flexibility.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top