Skip to the content

Project: pull out the emails

JavaScript · Unit 17: What JavaScript brings along

An email looks like this: name@site.com. With what you learned here you can describe it: \w+, the at sign, \w+, a dot and another \w+.

The dot goes in as \., because a loose dot means "any character".

const p = /\w+@\w+\.\w+/;
console.log(p.test("ava@mail.com"));
console.log(p.test("avamail.com"));

Prints

true
false

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Complete the code

    Fill in the blank in emailOk, which accepts the text only if it's a whole email and nothing else.

  2. 2. Write the code

    Write emails(text): an array with every email that shows up in the text, in the order they appear. If there are none, an empty array. Remember the g flag, and that match can give null.

  3. 3. Write the code

    Write domains(text): the sites of the emails in the text, with no repeats and in the order they appear. For ava@mail.com the domain is mail.com. The found array already carries the emails.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →