-2

I am working on automatic phone number highlight application in android. I have included javascript file in my android browser and used following code for rewriting the html document.

var regex = /\d{10}/g;
var text1234 = $("body:first").html();
text1234 = text1234.replace(regex, "<a href='tel:$&'>$&</a>");
$("body:first").html(text1234);

This code is working on some webpage which contains no javascript file and browser crashes with others. While commenting "$("body:first").html(text1234);" this line just page load without changes. How to solve this problem.

fazil tm
  • 299
  • 1
  • 5
  • 23
  • In what way does it crash? – Joseph Young Sep 10 '16 at 06:51
  • Browser crashes with webpages which have javascript content. Shows just loading and crashes up. – fazil tm Sep 10 '16 at 07:09
  • Gonna make a guess that it takes up too much memory running the regex. Infact, you do realise that your code replaces ALL 10 digits numbers in the HTML content? That is bound to fail at some point. `
    ` will break
    – Joseph Young Sep 10 '16 at 07:15
  • When i replace "$("body:first")" to "$("html:first")" then the pages are loaded but the page javascript functions (including the page functions) are not working. – fazil tm Sep 10 '16 at 07:40
  • See http://stackoverflow.com/q/1732348/1362755 why this is a bad idea. Use DOM APIs, not regex. – the8472 Sep 10 '16 at 08:21

1 Answers1

0

The answer is given below. it's workking as fine.

 var phonePattern = /\s((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}/g,
phoneReplacement = '<a href="tel:$&">$&</a>';

$(document).ready(function () {

    $('*','body').each(function() {
        $(this).html( $(this).html().replace(phonePattern,phoneReplacement)                );
    });

});
fazil tm
  • 299
  • 1
  • 5
  • 23