In this guide, I'll teach you how to trim the fat.

Let's say you've got an email address that looks like this: Joe Blow <joeblow@nomail.com>.

And all you want is the actual email address part. In other words, the joeblow@nomail.com piece.

I'll show you how to get that with the aid of a regular expression in TypeScript. That means you can easily use this solution in an Angular application.

Ready to go? Let's get started.

Express Yourself

To make this happen in my CRM Angular app, I created a service-level method, appropriately called getEmailAddressFromString():

  getEmailAddressFromString(emailAddress: string): string {
    let justEmail: string = emailAddress;

    if (emailAddress && emailAddress.indexOf("<") > -1) {
      let regex = /<(.*)>/g;
      let matches = regex.exec(emailAddress);

      if (matches.length > 0) justEmail = matches[1];
    }

    return justEmail;
  }

That method accepts an email address string (like the "Joe Blow" thing above) and returns only the email part of it.

First, the code sets the returned value to the value of emailString. That's a safety valve in case the email string passed in is already just joeblow@nomail.com or something similar.

Next, the code checks to make sure that the string includes a less-than sign. That means it's likely formatted in such a way that the actual email address needs to get pulled out of it.

And that's where the regular expression comes in to play.

That regex variable you see there sets the regular expression. It's bounded by slashes so TypeScript can parse it accordingly. No quotation marks needed.

The less-than/more than signs in the expression are searches for those characters in the string.

The parentheses in the regex is what's called a "capturing group." In this case, it grabs the contents between those less-than/more-than signs..

The period inside those parentheses means any character.

And the asterisk means any number of times.

So the regex will grab any character any number of times that's located between the less-than/more-than signs.

Finally: that g at the end indicates a global search. That means the regex will find all matches rather than stopping after the first match.

Keep in mind: regex is an object. Like many other objects, it's got a publicly available method that you can execute.

That method is imaginatively named exec().

Match Game

So what does exec() do?

It returns matches.

Specifically it returns an array of matches. 

The first element of that array is the part that matched: <joeblow@nomail.com>.

But you don't want that because you don't want those pesky less-than/more-than signs around your beautiful email address.

That's why there's a second element in that array.

The second element is the part that got captured. Remember those parentheses in the regex?

Sure you do. That second element, for the purposes of this exercise, includes just the stuff in between those less-than and more-than signs.

So the above method ultimately returns just joeblow@nomail.com by grabbing that second element in the array.

Note: the array uses 0-based indexing so the second element is matches[1].

Wrapping It Up

That's it. That's all there is to getting just the email address from a string.

Feel free to include something like the code snippet above in your own application source.

Have fun!

Photo by Liza Summer from Pexels