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 :)