0

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

ryan1234
  • 3
  • 1

1 Answers1

0

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~

BeRT2me
  • 12,699
  • 2
  • 13
  • 31
  • regexr is super helpful. Thanks, that really cleared things up for me. So, your suggestion works however it think i'm still missing something. When I Copy "+12345678910" and then use the designated TextExapnder snippet the return is nothing `""`. So I had the TE return `s2` like this:`var s2 = (""+TextExpander.pasteboardText).replace(/\D/g, ''); //var s2="+15302563702" var m = s2.match(/^\+1(\d{3})(\d{3})(\d{4})$/); if(m) `(${m[1]}) ${m[2]}-${m[3]}`; else s2;`. and it returned: 12345678910 without a "+" any idea? – ryan1234 Apr 15 '22 at 20:25
  • however, removing the `\+` solve my problem. – ryan1234 Apr 15 '22 at 21:10
  • Here `.replace(/\D/g, '')` you already remove the `+`, hadn't noticed that before. – BeRT2me Apr 15 '22 at 23:50