I have contact list and need to remove the country code(+91), spaces between number and zero(prefix with zero) from the mobile number. And it should contain only 10 digits.
I have tried using Regex in the following way, but it removing only spaces from the number.
var value = "+91 99 16 489165";
var mobile = '';
if (value.slice(0,1) == '+' || value.slice(0,1) == '0') {
mobile = value.replace(/[^a-zA-Z0-9+]/g, "");
} else {
mobile = value.replace(/[^a-zA-Z0-9]/g, "");
}
console.log(mobile);
JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.
trim() The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.)
In this article, we’ll look at how to remove zero-width space characters from a JavaScript string. To remove zero-width space characters from a JavaScript string, we can use the JavaScript string replace method that matches all zero-width characters and replace them with empty strings.
Then you can remove it using your preferred way like String PhoneNumber = phoneNo.replaceAll ("countryCode ", "" ); Thanks for contributing an answer to Stack Overflow!
The second string can be given as empty string so that the empty space to be replaced. The first parameter is given a regular expression with a space character (” “) along with the global property. This will select every occurrence of space in the string and it can then be removed by using an empty string in the second parameter.
How to remove leading zeros from a number in JavaScript? How to remove leading zeros from a number in JavaScript? Use parseInt () to remove leading zeros from a number in JavaScript.
var value = "+91 99 16 489165";
var number = value.replace(/\D/g, '').slice(-10);
You could use a string.substr if u know for sure theres a country code after a "+" or "0".
var value="+91 99 16 489165";
var mobile = '';
if(value.charAt(0) == '+' || value.charAt(0)=='0'){
mobile = value.replace(/[^a-zA-Z0-9+]/g, "").substr(3);
}
else {
mobile = value.replace(/[^a-zA-Z0-9]/g, "");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With