I have a paragraph with some address data, and now I want to replace with regex email and phone number to clickable link.
<div class="container">
<p id="contactInfo">
Address 27, Tokio 679 01<br>
Tel: 0909000000<br>
E-mail: exampplet@example.com
</p>
</div>
<script>
var contact_info = document.getElementById('contactInfo');
var html = contact_info.innerHTML;
if (html) {
var regex = /([a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4})/ig;
contact_info.innerHTML = html.replace(regex, '<a href="mailto:$1">$1</a>');
}
</script>
now the email will change to a clickable link, but how can I change phone number to clickable link too?
I tried this: but it never working:
<script>
var contact_info = document.getElementById('contactInfo');
var html = contact_info.innerHTML;
if (html) {
var pattern = /^\s*(?:\+?(\d{1,3}))?[\W\D\s]*(\d[\W\D\s]*?\d[\D\W\s]*?\d)[\W\D\s]*(\d[\W\D\s]*?\d[\D\W\s]*?\d)[\W\D\s]*(\d[\W\D\s]*?\d[\D\W\s]*?\d[\W\D\s]*?\d)(?: *x(\d+))?\s*$/g;
contact_info.innerHTML = html.replace(pattern, '<a href="tel:$1$2$3$4$5">$1$2$3$4$5</a>');
}
</script>
Here is codepen example: enter link description here
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment