javascriptregexgoogle-chrometel

Regex - match '%20' in encoded tel URL


I have the following regex:

 (?:[\s:]|\d+(?:-|.)|^)(?(\d{3}))?[- .]?(\d{3})[- .]?(\d{4})(?=<|\s|$)

When copying the regex it looks that some escape backslashes got lost, so here is the correct regex: (?:[\s:]|\d+(?:-|\.)|^)\(?(\d{3})\)?[- \.]?(\d{3})[- \.]?(\d{4})(?=<|\s|$)

It is used in Tel Linker extension on Chrome to correctly format the telephone number links on a web page.

However, it fails to properly format a tel link when the empty spaces in the URL are encoded with '%20': e.g.:

https://someresource.com/tel:(904)%20640-8301
https://someresource.com/tel:(904)%20640%208301

Unfortunately, I have no experience in working with Regex so any help/suggestions are highly appreciated.


Solution

  • As @mplungjan pointed out, your regex is invalid.

    Try this:

    (?:[\s:]|\d+(?:-|.)|^)\(?(\d{3})\)?(?:%20)*(?:[- .]+(?:%20)*)?(\d{3})(?:(?:%20)*[- .]*(?:%20)*)?(\d{4})(?=<|\s|$)
    

    If you use this as a string, you'll need escape every \:

    (?:[\\s:]|\\d+(?:-|.)|^)\\(?(\\d{3})\\)?(?:%20)*(?:[- .]+(?:%20)*)?(\\d{3})(?:(?:%20)*[- .]*(?:%20)*)?(\\d{4})(?=<|\\s|$)
    
    

    Tested against:

    https://someresource.com/tel:(904)640-8301
    https://someresource.com/tel:(904)%20640%208301
    https://someresource.com/tel:(904)%20640-8301
    https://someresource.com/tel:(904)%20640 - 8301
    https://someresource.com/tel:(904)%20640%20-%208301
    

    Thanks @Casimir et Hippolyte for the suggestions