Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got a task to find phone numbers in the page and wraps them with an a tag . It needs to find a variety of different phone formats so please choose a regex that is flexible and comprehensive.
Examples -

0116 2746 190

0116 2746190

079155 24272

+44 78904 12345

1234567890

(0800) 123 456

I've tried with below code, its not working.. Please help

C#
$(document).ready(function () {
var regex = /((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/g;
var text = $("body:first").html();
text = text.replace(regex, "<a href=\"tel:$&\">$&</a>");
$("body:first").wrap(text);
});
Posted
Updated 20-Aug-21 22:38pm
v6
Comments
Andreas Gieriet 5-Aug-15 13:14pm    
What means "not working"?
Syntax error while compiling?
Not matching any, no matching all?
Please use the "Improve question" link to give more details.
Regards
Andi
Member 11887622 6-Aug-15 4:06am    
Hi Andi,

Above reg expression was not matching the above formats at all.

Take a look at http://www.area-codes.org.uk/[^]. Unfortunately the UK telephone numbering system has never been rationalised properly. So there are still quite a few different formats, and even numbers with different digit counts.

 
Share this answer
 
How strict must the regex be?
For the above given numbers, I'd go for a pattern match as follows:
ukphone       = [ prefix ] suffix | special .

prefix        = international | zero .
suffix        = first part part part part part part part part part .
special       = group part { part } .

international = '+' first part .
group         = '(' first { part } { ' ' } ')' .
first         = { ' ' } digit .
part          = { ' ' | '-' } digit .
digit         = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' .
zero          = '0' .


You may now translate this into a Regex.
First pseudo-code:
^\s*((?:[+]<first><part>|0)?<first><part>{9}|[(]<first><part>+\s*[)]<part>+)\s*$
and replace <first> and <part> by
first: (?:\s*\d)
part:  (?:[-\s]*\d)
This is a rather relaxed matcher:
- several separators in a row is accepted
- special numbers are not checked for proper length
- ...
If you want disallow multiple separators in a row, use:
first: (?:\s?\d)
part:  (?:[-\s]?\d)


Have fun!
Cheers
Andi

[EDIT]
Initially the question was tagged C#3.5, so my suggested solution was for C#.
Now it is tagged JQuery.
Nonetheless: the following Regex works for C# and will probably too for JQuery:
C#
Regex rex = new Regex(@"^\s*(([+]\s?\d[-\s]?\d|0)?\s?\d([-\s]?\d){9}|[(]\s?\d([-\s]?\d)+\s*[)]([-\s]?\d)+)\s*$");
string[] data = new string[]
{ "0116 2746 190"
, "0116 2746190"
, "079155 24272"
, "+44 78904 12345"
, "1234567890"
, "(0800) 123 456"
, "0116 2746190"
};
foreach (string number in data)
{
    Console.WriteLine("match({0}) = {1}", number, rex.Match(number).Success); 
}

[/EDIT]
 
Share this answer
 
v3
Comments
Member 11887622 6-Aug-15 4:07am    
Andi, above expression written by you is not matching the number formats. :(
Andreas Gieriet 6-Aug-15 5:33am    
Please show me your pattern.
Andi
Member 11887622 6-Aug-15 5:34am    
<script>
$(document).load(function () {
var regex = /^\s*((?:[+](?:\s?\d)(?:[-\s]?\d)|0)?(?:\s?\d)(?:[-\s]?\d){9}|[(](?:\s?\d)(?:[-\s]?\d)+\s*[)](?:[-\s]?\d)+)\s*$/;
var text = $("body:first").html();
text = text.replace(regex, "$&");

$("body:first").wrap(text);
});
</script>
Andreas Gieriet 6-Aug-15 5:49am    
You tagged C#3.5, but your sample is not C#3.5. My Regex is for C#.
I guess if you remove all ?:, the syntax is matching.
Andi
Member 11887622 6-Aug-15 5:53am    
Sorry, it is for JQuery

I removed all ?:

var regex = /^\s*(([+](\s?\d)([-\s]?\d)|0)?(\s?\d)([-\s]?\d){9}|[(](\s?\d)([-\s]?\d)+\s*[)]([-\s]?\d)+)\s*$/;

Its still not picking the values
0116 2746190
079155 24272
+44 77013 12345
1234567890
(0800) 123 456

Any ideas Andi?
This is the solution, Finally!

HTML
<div id="departments">
    <ul>
        <li>Department 1 - 0116 2746 190</li>
        <li>Department 2 - 0116 2746 190</li>
        <li>Department 3 - 079155 24272</li>
        <li>Department 4 - +44 78904 12345</li>
        <li>Department 5 - 1234567890</li>
        <li>Department 6 - (0800) 123 456</li>
        <li>Department 7 - 0116 2746190</li>
    </ul>
</div>



HTML
var regex = /\s*(([+](\s?\d)([-\s]?\d)|0)?(\s?\d)([-\s]?\d){9}|[(](\s?\d)([-\s]?\d)+\s*[)]([-\s]?\d)+)\s*/; 

var text = $("body").html();

var textNodes = $("body").find('*').contents().filter(function(){
    if(regex.test(this.nodeValue)){
        var anchor = document.createElement('a');
        anchor.setAttribute('href', 'tel:');
        anchor.appendChild(document.createTextNode(this.nodeValue.match(regex)[0]));        
        this.parentElement.appendChild(anchor);
        this.nodeValue = this.nodeValue.replace(regex, '');
    }    
});
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900