I'm trying to implement a javascript function to replace country code part of a phone number.
The input is +90 (533) 333 33 33 and I want to replace +90 part with javascript. I tried do write a regex but I couldn't succeed.
/^\++[a-z]+\s$/
EDIT : Final solution
$("#ddlCountry").change(function () {
if ($("#tMobile").val() == '') {
$("#tMobile").val("+" + $(this).find(":selected").attr("CountryCode")
+ " ");
} else {
$("#tMobile").val($("#tMobile").val().replace(/^(\+\d*)/,
"+" + $(this).find(":selected").attr("CountryCode")));
}
});
I'm trying to implement a javascript function to replace country code part of a phone number.
The input is +90 (533) 333 33 33 and I want to replace +90 part with javascript. I tried do write a regex but I couldn't succeed.
/^\++[a-z]+\s$/
EDIT : Final solution
$("#ddlCountry").change(function () {
if ($("#tMobile").val() == '') {
$("#tMobile").val("+" + $(this).find(":selected").attr("CountryCode")
+ " ");
} else {
$("#tMobile").val($("#tMobile").val().replace(/^(\+\d*)/,
"+" + $(this).find(":selected").attr("CountryCode")));
}
});
Share
Improve this question
edited Jan 2, 2012 at 3:21
HasanG
asked Jan 2, 2012 at 2:43
HasanGHasanG
13.2k31 gold badges102 silver badges159 bronze badges
3
-
2
Why do you use
[a-z]
when you're matching numbers? – Sergio Tulentsev Commented Jan 2, 2012 at 2:45 -
That regular expression would match one or more
+
followed by one or morea-z
followed by exactly one piece of whitespace. I don't see how it could match90
. Nor does it include any mands to do any replacement. Can you include more of your code? This doesn't look anywhere near enough... – sarnold Commented Jan 2, 2012 at 2:46 - Because [0-9] didn't work also.. I'm not good with regular expressions. – HasanG Commented Jan 2, 2012 at 2:48
3 Answers
Reset to default 2The following pattern should match any country code, assuming there's always a non-digit character between the country code and whatever number es next: /^(\+\d*)/
var phoneNumber = "+90 (533) 333 33 33";
phoneNumber = phoneNumber.replace(/^(\+\d*)/, '+852');
alert(phoneNumber);
(Try it on JSFiddle)
Edit: Okay... this is a bit silly, you can also do this:
var phoneNumber = "+90 (533) 333 33 33";
phoneNumber = phoneNumber.replace(/^(\+)(\d*)(.*)/, '$1852$3');
alert(phoneNumber);
(Try it on JSFiddle)
I was trying to make it unnecessary to include the plus sign in the new country code, but I can't find a way of doing that other than what I've shown above. Essentially, it uses three capture groups: one for the plus sign, one for the country code, and one for the rest of the phone number. In the new country code, I sandwiched the country code itself in between $1
and $3
, which translates to:
Replace the old phone number with a new phone number consisting of the first capture group (the plus sign) followed by the new country code ("852") followed by the rest of the phone number (" (533) 333 33 33").
as long as you can guarantee that your input will be in that format:
var phone = '+90 (533) 333 33 33';
phone.replace(/^\+[0-9]{2}/,'xyz')
Before: +90 (533) 333 33 33
After: xyz (533) 333 33 33
This is considerably faster than regular expressions.
function replaceCountryCode ( number, replaceWith ) {
return replaceWith + number.substr( number.indexOf( ' ' ) );
};
Demo: http://jsfiddle/ThinkingStiff/XEBdP/
Performance: http://jsperf./regex-vs-indexof-replace