0

I'm loading a page with AJAX and would like to use Jquery to search for phone numbers (whose format is 555.555.5555) on the loaded page and wrap them with tel links:

<a href="tel:phone-number-here"> </a>

I know the parts I need to accomplish this (I think): Regex and .wrap(). I'm just not sure how to piece them together with the code I've got. I'm also not sure how to get Jquery to retain found numbers and insert them inside the tel links. Would I have to use each and $(this) with variables?

Here's the regex I found for identifying 7 - 10 digit numbers. I want to leave it open to various delimiters in case our site stops using periods in the future:

^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

And here's the code I'm using to load each page:

 $(".services").click(function(){  
    var loadUrl = "http://www.example.com";      
    $("#content").html(ajax_load).load(loadUrl + " #content");  
 });

Any help would be appreciated.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
qp2wd
  • 33
  • 1
  • 5
  • Don't most phones do this on their own? – Chuck Jan 05 '11 at 19:06
  • Do they? I wasn't sure. If that's true it'll save me mountains of time, because I've been poking at this code for an hour and a half trying to get it to work. If phones do it automagically, I'm not even gonna bother. – qp2wd Jan 05 '11 at 19:33
  • There's no need for this. I would say that most phones capable of dialing numbers found online have the ability to do so without requiring you to go through this effort. – js1568 Jan 05 '11 at 20:23

1 Answers1

0

This question will show you how to get all the elements on a page with a certain string. You could probably use a regex as well. Then you could do string replacement using your regex on all the found elements.

Community
  • 1
  • 1
Samo
  • 8,202
  • 13
  • 58
  • 95
  • I tried this: var num = $('*:contains("^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$")'); num.wrap(''); But nothing happened. I don't really know javascript, so I'm not sure how I ought to be structuring the regex request or the subsequent wrap to make it do something. – qp2wd Jan 05 '11 at 18:50
  • @qp2wd: a regex doesn't typically go in quotes. If you put it in quotes, it will look for that exact string. Generally a regex looks like `var myRegex = /foo/`. Assign it to a variable like that and then do `$("*:contains(myRegex)")`. I've never tried this so I don't know for sure if it will work, you'll have to play around with it or search the jQuery docs for a better solution. – Samo Jan 05 '11 at 18:54