From an email address like something@gmail.com
I want to fetch domain name gmail.com
.
i want to use that pattern on textbox value in Javascript.
A bit cleaner and up-to-date approach:
const email = "something@gmail.com"
const domain = email.includes('@') && email.split("@").pop()
Domain will be false
if email doesn't contain @ symbol.
Why not just do this.
var email = "something@gmail.com", i = email.indexOf("@");
if (i != -1) {
email = email.substring(i);
}
Regex isn't really required, you could also go email = email.split("@")[1];
Using a simple string split won't work on addresses like 'abc@abc'@example.com
which is a valid address (technically). I believe splitting on @
and taking the last element should be fine, because no @
characters are allowed to appear in the domain.
Or, since you requested, a regex:
[^@]+$