Created
July 5, 2023 01:51
-
-
Save edcottrell/131766eb0a500e9c970c070822243e81 to your computer and use it in GitHub Desktop.
TextExpander Snippet for Creating and Formatting Telephone Number Links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start with the clipboard content | |
var result = TextExpander.pasteboardText; | |
// Use ONE of the following three code lines to format the number | |
// As xxx-xxx-xxxx | |
result = result.replace(/^[(). –-]*(\d\d\d)[(). –-]*(\d\d\d)[(). –-]*(\d\d\d\d)[(). –-]*(?: *x *(\d+))?$/, '[$1-$2-$3 x$4](tel:$1$2$3p$4)'); | |
// As xxx.xxx.xxxx | |
// result = result.replace(/^[(). –-]*(\d\d\d)[(). –-]*(\d\d\d)[(). –-]*(\d\d\d\d)[(). –-]*(?: *x *(\d+))?$/, '[$1.$2.$3 x$4](tel:$1$2$3p$4)'); | |
// As (xxx) xxx-xxxx | |
// result = result.replace(/^[(). –-]*(\d\d\d)[(). –-]*(\d\d\d)[(). –-]*(\d\d\d\d)[(). –-]*(?: *x *(\d+))?$/, '[($1) $2-$3 x$4](tel:$1$2$3p$4)'); | |
// Strip empty extensions from the link text | |
result = result.replace(/ x\]/, ']'); | |
// Strip empty extensions from the tel: URI | |
result = result.replace(/p\)/, ')'); | |
// Return the result | |
TextExpander.appendOutput(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this is currently limited to United States numbers, and I have provided three options for how to format the output. Adaptation to non-US numbers should be fairly straightforward, unless you want to support multiple national formats, which is much more complex.