Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 141277

Add a prefix of +1 to a string

$
0
0

How can I remove all non 10 digit numbers from a string, and add a +1 to the front of it if it doesn't exist yet?

Here's the regex code I have:

phoneNumber.replace(/[^0-9.\,]/g, '');
phoneNumber = '+1' + phoneNumber;

but this won't work if phoneNumber already has +1. How can I make it that the regex first removes all non numerical, and adds a prefix of +1?


Viewing all articles
Browse latest Browse all 141277

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>