Search code examples
javascripthtmltel

How to wrap any mobile number's into Anchor tag using javascript


I have one html page where all data's are coming dynamically with some mobile numbers. How can I wrap all mobile numbers into anchor tag using javascript.

Example: <a href="tel:+91 9999999999">+91 9999999999</a>

HTML:

<p>Tel: +022 23334976</p>
<p>Tel: +91 8976817722 </p>
.
.
.
.
<p><b>Tel:</b> +91 8976817725 </p>

Please help me to write common js for handling above type of html page.


Solution

  • Try this:

    var body = document.getElementsByTagName('body')[0];
    body.innerHTML = body.innerHTML.replace(/(?:<b>)?Tel:(?:<\/b>)? ?(\+[0-9]* [0-9]*)/ig, '<a href="$1">$1</a>');