Last updated: Mar 4, 2024
Reading timeยท3 min
To extract the name from an email address:
split()
method to split the email on the @
symbol.const email = 'bobbyhadz@email.com'; const result = email.split('@')[0]; console.log(result); // ๐๏ธ "bobbyhadz"
The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.
The String.split()
method takes the following 2 parameters:
Name | Description |
---|---|
separator | The pattern describing where each split should occur. |
limit | An integer used to specify a limit on the number of substrings to be included in the array. |
We split the string on the @
symbol.
const email = 'bobbyhadz@email.com'; // ๐๏ธ [ 'bobbyhadz', 'email.com' ] console.log(email.split('@')); // ๐๏ธ bobbyhadz console.log(email.split('@')[0]);
The first string in the array contains the username which we can access using an
index of 0
.
0
, and the index of the last element is array.length - 1
.If you have to do this often, define a reusable function.
function extractNameFromEmail(email) { return email.split('@')[0]; } const email = 'bobbyhadz@email.com'; const name = extractNameFromEmail(email); console.log(name); // ๐๏ธ bobbyhadz
The function takes an email as a parameter and extracts the name from the email.
@
symbolIf an @
symbol is not contained in the string, you'd get an array containing
the entire string as a parameter.
const email = 'bobbyhadzemail.com'; // ๐๏ธ [ 'bobbyhadzemail.com' ] console.log(email.split('@'));
If you need to handle this scenario, use an if
statement.
const email = 'bobbyhadzemail.com'; let name = ''; if (email.includes('@')) { name = email.split('@')[0]; } console.log(name); // ๐๏ธ ""
The String.includes()
method returns true
if the supplied substring is contained in the string and
false
otherwise.
We only reassign the name
variable, if the @
symbol is contained in the
string.
An alternative approach is to use the String.slice()
method.
String.slice
This is a two-step process:
String.indexOf()
method to get the index of the @
character.slice()
method to get the part of the string up to the index.const email = 'bobbyhadz@email.com'; const result = email.slice(0, email.indexOf('@')); console.log(result); // ๐๏ธ "bobbyhadz"
The String.slice() method extracts a section of a string and returns it, without modifying the original string.
The String.slice()
method takes the following arguments:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
The slice starts at index 0
and goes up to, but not including the index of the
@
character.
We used the String.indexOf()
method to get the index of the @
character in
the string.
const email = 'bobbyhadz@email.com'; console.log(email.indexOf('@')); // ๐๏ธ 9
The String.indexOf() method returns the index of the first occurrence of a substring in a string.
If the substring is not contained in the string, the method returns -1
.
You can learn more about the related topics by checking out the following tutorials: