Search code examples
javascripttextexpander

Text expander paste function to format a phone number


I am trying to create a Text Expander snippet to speed up a workflow. I want to copy a number in this format: +12345678910 and be able to past this it like this: (234) 567-8910 and make the font bold.

The closest script I've found is:

var s2 = (""+TextExpander.pasteboardText).replace(/\D/g, '');
var m = s2.match(/^(\d{3})(\d{3})(\d{4})$/);
if(m) `(${m[1]}) ${m[2]}-${m[3]}`;
else "";

This allows me to copy the "2345678910" portion of the data and and have it pasted as (234) 567-8910. Any suggestions would be super helpful, I'm interested in learning.

thanks


Solution

  • Add the +1 to your regex, but don't include it in a group: /^\+1(\d{3})(\d{3})(\d{4})$/

    regexer can be super helpful for flushing these issues out~