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

If you loved this and want more like it, sign up!


Did I make your day?
Buy me a coffeeBuy me a coffee  

(you can learn about the bigger picture I'm working towards on my patreon page )



Published

2014-2-17



Buy me a coffeeBuy me a coffee


  73% of Goal Reached


Want more like this?