简体   繁体   中英

Copy Only Domain Name from email input

I have an input field for email input and URl input field. On double click i copy mail's value to url like this

<script>
   function cp_mail() {
     document.getElementById("mail").value = document.getElementById("Url").value;
   }
 </script>  

Now i only want to copy domain name of email (from the "@" to the end) and insert on the begining of Url "www." .Any Ideas ?

Simply try this

var domainName = emailId.split("@").pop();

or

var domainName = emailId.split("@")[1];

This will split the emailId by @ which will return the array of 2 items, one before @ and one after @ . Second item (or last item) is what you are looking for.

Now compute the URL as

var url = "www." + domainName;

DEMO

 var emailId = "abc@domainname.com"; var domainName = emailId.split("@")[1]; var url = "www." + domainName; alert(url);

you can write like this

var email="test@domain.com";
var domainName = 'www.' + email.split('@')[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM