Regex search Telefon but skip urls

Viewed 55

I have a regex that works perfectly with the numbers I want to get, but the problem is with the URLs that also get them, how could I do to remove them?

Skip if there is no blank space, line break or the like?

Thanks !!

my current regex is:

/((?(0{1,2}|+)\d{1,2})?)?(([ -]*\d+){8,20})+/gm

https://regex101.com/r/yzAScj/1

the modification I made doesn't work:

/\s{1}((?(0{1,2}|+)\d{1,2})?)?(([ -]*\d+){8,20})+\s{0,1}/gm

https://regex101.com/r/yzAScj/2

Test text:

https://asd.com/20441235534-aaaaaaaaaaa-

202460676

aasdasd 202460676

https://asd.com/20441235534


202460676-



10 text
1234 text
text 123

00491234567890
+491234567890

0123-4567890

0123 4567 789
0123 456 7890
0123 45 67 789

+490123 4567 789
+490123 456 7890
+49 123 45 67 789

123 4567 789
123 456 7890
123 45 67 789


+49 1234567890
+491234567890

0049 1234567890
0049 1234 567 890

(0049)1234567890
(+49)1234567890

(0049) 1234567890
(+49) 1234567890



text text (0049) 1234567890 text text
text text (+49) 1234567890 text text

makes the phone numbers have the link "tel:" to be able to click on them.

You shouldn't pick a URL as a phone.

JS code (with jquery):

function searchAndReplacePhones(){

    var regex =  /(\(?(0{1,2}|\+)\d{1,2}\)?)?(([ -]*\d+){8,20})+/gm;

    //Beschreibung
    $(".my_text").html($(".my_text").html().replace(regex, " <a href=\"tel:$&\">$&</a> "));

}
2 Answers

You may use a regex that matches either a URL (say, with https?://\S*) or matches and captures the phone number:

var regex = /https?:\/\/\S*|((?:\(?(?:0{1,2}|\+)\d{1,2}\)?[ -]*)?\d(?:[ -]*\d){7,19})/gi;

Then, when you use it in the .replace method, you need to use a callback method, where you pass the regex match and analyze the structure of the match: if Group 1 matched, replace it, otherwise, put back the match value.

See the regex demo and a JS demo below:

var text = "\n\nhttps://asd.com/20441235534-aaaaaaaaaaa-\n\n202460676\n\naasdasd 202460676\n\nhttps://asd.com/20441235534\n\n\n202460676-\n\n\n\n10 text\n1234 text\ntext 123\n\n00491234567890\n+491234567890\n\n0123-4567890\n\n0123 4567 789\n0123 456 7890\n0123 45 67 789\n\n+490123 4567 789\n+490123 456 7890\n+49 123 45 67 789\n\n123 4567 789\n123 456 7890\n123 45 67 789\n\n\n+49 1234567890\n+491234567890\n\n0049 1234567890\n0049 1234 567 890\n\n(0049)1234567890\n(+49)1234567890\n\n(0049) 1234567890\n(+49) 1234567890\n\n\n\ntext text (0049) 1234567890 text text\ntext text (+49) 1234567890 text text";
var regex = /https?:\/\/\S*|((?:\(?(?:0{1,2}|\+)\d{1,2}\)?[ -]*)?\d(?:[ -]*\d){7,19})/gi;
//https://regex101.com/r/0LxWTv/5
document.body.innerHTML = "<pre>" + text.replace(regex, function($0,$1) {
  return $1 ? '<a href="tel:' + $1 + '">' + $1 + '</a>' : $0;
} ) + "</pre>";

Note I modified the pattern a bit:

  • (?:\(?(?:0{1,2}|\+)\d{1,2}\)?[ -]*)? - an optional non-capturing group that matches 1 or 0 occurrences of
    • \(?
    • (?:0{1,2}|\+) - one or two zeros or +
    • \d{1,2} - one or two digits
    • \)? - an optional )
    • [ -]* - 0 or more spaces or hyphens
  • \d - a digit
  • (?:[ -]*\d){7,19} - seven to nineteen digits separated with 0 or more spaces or hyphens.

PHP regex, skip <link> tags when rel="canonical" shows the use of (*SKIP)(*F) trick, add https?:\/\/\S*(*SKIP)(*F)| to your expression:

https?:\/\/\S*(*SKIP)(*F)|(\(?(0{1,2}|\+)\d{1,2}\)?)?(([ -]*\d+){8,20})+

See proof

With https?:\/\/\S*(*SKIP)(*F)|, you skip URLs, match anywhere else.

Related