javascript

Get text before @ in an email using Javascript

Before we get into how to get the text before the @ in an email using Javascript, it's important to understand what an email address is. An email address is made up of two parts: the username and the domain. The username is the part of the email address that comes before the @ symbol, and the domain is the part that comes after. So, how can we get the text before the @ in an email using Javascript? The answer is quite simple. We can use the indexOf() method to find the position of the @ symbol in the email address, and then use the substring() method to get the text before it.

const email = "[email protected]"

const result = email.substring(0, email.indexOf("@"));

console.log(result);

Output

johndeo05

Try it yourself

The code above is an example of using the substring method on a string to extract the part of the string before the "@" character. The substring method takes two arguments, the first is the index to start extracting from and the second is the index to stop extracting at. The indexOf method is used to find the index of the "@" character.

More solutions to get the text or username from an email address are as below:

Solution 1: Using substring() function

The substring() function in JavaScript can be used to get the text before the "@" symbol in an email address. This can be useful when you want to extract the username from an email address. We will also use the indexOf() function of Javascript to get the position of @.

Syntax

email.substring(0, email.indexOf("@"));

Code example

const email = "[email protected]"

const result = email.substring(0, email.indexOf("@"));

console.log(result);

Output

test_user

Try it yourself

Solution 2: Using split() function

If you want to get the text before the @ symbol in an email address using the split() function in JavaScript, you can do so by splitting the string at the @ symbol and then taking the first element of the resulting array.

Syntax

email.split('@')[0]

Code example

const email_str = "rajesh_005@email_domain.com";

const username = email_str.split('@')[0];

console.log(username);

Output

rajesh_005

Try it yourself

This code is using the split method on the email string to create an array with two elements, the first element being the username ( everything before the @ symbol) and the second element is the domain name. It then accesses the first element of that array (the username) and prints it to the console.

Solution 3: Using regex and match() function

The match() function can be used in conjunction with a regular expression to find the text before the @ sign in an email address. When used with the global flag set, the match() function will return an array of all matches, with the text before the @ sign in the first element.

Syntax

email.match(/^(.+)@/)[1];

Code example

const email_str= "[email protected]";

const result = email_str.match(/^(.+)@/)[1];

console.log(result);

Output

rick_neo

Try it yourself

The code above is using a regular expression to match a pattern in the email_str string. The pattern is anything that is before the "@" symbol. The match method returns an array with the matches. In this case, the result is just the first element of that array, which is everything before the "@" symbol in the email_str string.

Was this helpful?