Home > Enterprise >  String method, select from email only username
String method, select from email only username

Time:02-04

it is possible to grab from email only data until @gmail.com ?

for example:

[email protected] 

I want to select only

myemail

It should be aplycable for any emails. Any length of emails.

if I have emails:

[email protected] and [email protected]

it should give me:

myemail11 and myem 

I thing req expreson will be good for that , but I am not goog in reg expr.

CodePudding user response:

The simplest way is by getting string before last @ by using substring:

const address = "[email protected]";
const username = address.substring(0, address.lastIndexOf("@"));

console.log(username)

CodePudding user response:

Apart from the substring, you can also split the email and take the first index value.

const email = "[email protected]";
const name = email.split('@')[0]

console.log(name)
  • Related