2

I'm currently building a chat box and wanted to do some quick client-side validation of what users are sending. I want to be able to detect if the user sent a phone number in their message (in as many formats as possible, but general US formats would be okay) and replace it with pound signs (#). I have this implemented for emails like so:

var message = 'Hi. This is my email: foo@bar.com. This is my number: (970)-555-7595.';
var emailExp = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/img;

// prints: Hi. This is my email: *********. This is my number: (970)-555-7595
console.log(message.replace(emailExp, '*********'));

What I want it to look like is this:

var message = 'Hi. This is my email: foo@bar.com. This is my number: (970)-555-7595.';
var emailExp = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/img;
var phoneExp = SOME_CRAZY_PHONE_REGEX;

// prints: Hi. This is my email: *********. This is my number: ##########
console.log(message.replace(emailExp, '*********').replace(phoneExp, ##########));

Any way I can accomplish this? I need it to find all instances of every phone number in the string and replace them with the pound sign. The expression doesn't have to be too crazy, but I'd like it to match most general US formats.

I did do some looking around for patterns but most of them validate that a complete string is a phone number. I'm trying to find and replace all phone numbers in a string. Thanks

EDIT

I'm not looking to improve my email pattern. Just a pattern for phone numbers that will work in this context :)

Marcus Longwell
  • 111
  • 1
  • 8
  • so same reg exp that matches if it is phone number is almost the same thing you need... – epascarello Sep 09 '16 at 19:01
  • 1
    Find one of the codes that is validating phone numbers and just take the regex portion out of it instead of using the entire function. Plug it into your second snippet just as you do with `emailExp` and you should be good to go. That being said, it's never a great idea to use regex for phone numbers. You'd be better off using a library like this: https://github.com/googlei18n/libphonenumber – Tyler Roper Sep 09 '16 at 19:01
  • email regex fails if you have stuff like non-latin TLDs or if you send `email+string@gmail.com`. As for phone number - that is going to be one crazy regex. What are you trying to accomplish with this? Because if you are trying to protect from anybody entering email and/or phone number users will immediately bypass those measures by, for example spelling out stuff `emal at domain.com` or even as simple as adding a random space that bypasses your regex. – VLAZ Sep 09 '16 at 19:04
  • 1
    What are you looking for here? A phone number regex? Whatever regex you use, the global flag will replace every occurrence of it in a string. – ryanlutgen Sep 09 '16 at 19:06
  • @TylerRoper I've read such. However I don't need this to be super duper good. If people want to get around it they will and that's fine. It's mostly just to try and keep people from violating our TOS. If they choose to circumvent it, then they choose to circumvent it. Thus, I don't want to load in an entire library if this can be solved by a line of code. – Marcus Longwell Sep 09 '16 at 19:06
  • Is that a real telephone #? – IMTheNachoMan Sep 09 '16 at 19:07
  • @MarcusLongwell Then just do a lookup on google for a phone number regex and pop that sucker into your second snippet. – Tyler Roper Sep 09 '16 at 19:07
  • You can make your email regex a lot simpler — `/([\w.-]+@[\w.-]+\.[\w.-]+)/g;` will work just as well. – r3mainer Sep 09 '16 at 19:08
  • @IMTheNachoMan Already linked that in a previous comment friend. – Tyler Roper Sep 09 '16 at 19:10
  • @TylerRoper I've tried doing that on a few that I've found here: http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation and on some other questions. I'm not great at regex, but it seems like most of them validate an entire string as a phone number, which doesn't work in this case. – Marcus Longwell Sep 09 '16 at 19:11
  • @TylerRoper: But mine was prettier. :P Heh. Sorry. Just saw that. I'll delete. – IMTheNachoMan Sep 09 '16 at 19:11
  • 1
    @IMTheNachoMan No. 555 number. – Marcus Longwell Sep 09 '16 at 19:13
  • For email regex, there's IDN (unicode stuff), so `/([\w.-]+@[\w.-]+\.[\w.-]+)/` is wrong. That was just valid for plain ASCII from 90's – Gilles Quénot Sep 09 '16 at 19:19

2 Answers2

6

The solution ended up being:

var phoneExp = /(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?/img;

As per fatcat's resolution fatcat1111's solution here. I just needed to remove the beginning ^ character and the ending $ character.

Community
  • 1
  • 1
Marcus Longwell
  • 111
  • 1
  • 8
2
var phoneExp = /(\(\d{3}\))?[\s-]?\d{3}[\s-]?\d{4}/img;

this regex should work for US phone numbers with or without area code and wit or without - on the places they should go.

lsaadev
  • 82
  • 1
  • 10