Hey! I'm available for hire (and I appreciate tips)  



Coders must see this 1 weird old regex to reduce garbage pasted from a PDF into properly captured phone numbers

See it in action: http://cdpn.io/eDAKh

http://codepen.io/coolaj86/pen/eDAKh

match-phone-numbers.js:

// See it in action: http://cdpn.io/eDAKh
// http://codepen.io/coolaj86/pen/eDAKh
var reg = /(\+?1)?\s*[\-\.]?\s*\(?\s*(\d{3})\s*\)?\s*[\-\.]?\s*(\d{3})\s*[\-\.]?\s*(\d{4})(?=\D|$)/g
  , result
  , text
  , lines
  ;

lines = [
  "+1 801 360 1111"
, "1 (801) 360 2222"
, "(801) 360-3333"
, "7578804444"
, "17578805555"
, "+17578806666"
, "+1 (801) 360-7777"
, "1.801.360.8888"
, "801.360.9999"
];

text = lines.join(' ');

while ((result = reg.exec(text)) !== null) {
  console.log(result);
}

I don't even want to break that down... but it works.

You can take some weird table from a PDF that pastes as garbage with the columns all crazy interspersed (you know the PDFs I'm talking about - like, all of them) and get back all of the phone numbers.

Of course it works for tables from CSVs and such that paste properly as well, but those, well, you don't have to RegExp the devil out of them to get them to work for you.


By AJ ONeal

Was this useful to you? Share it!

Also, you can give me a tip or hire me.


Published

2014-2-17

Categories


Tags