Discussion:
RegExp problem
(too old to reply)
Ron Hinds
16 years ago
Permalink
What am I doing wrong with this code? I'm trying to parse a phone number
down to just the digits, irrespective of how the user enters it.

var phone;
var re;

phone = document.Register.Phone.value;
re = new RegExp("\D", "ig");
phone = phone.replace(re, "");

alert("Phone=" + phone);

The alert always shows the value of phone to be the same as whatever I typed
in the form, e.g., (702) 555-1212.
Igor Tandetnik
16 years ago
Permalink
Post by Ron Hinds
What am I doing wrong with this code? I'm trying to parse a phone
number down to just the digits, irrespective of how the user enters
it.
var phone;
var re;
phone = document.Register.Phone.value;
re = new RegExp("\D", "ig");
You need "\\D" (two backslashes), for the same reason you needed a
backslash in '\'' . Or, use regexp literal:

re = /\D/ig;
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Ron Hinds
16 years ago
Permalink
...
Thanks, you did it again!

Loading...