Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression - have to insert extra"\" 1

Status
Not open for further replies.

patlv23

Programmer
May 23, 2002
31
PH
Hi!

I'm new to regexp and javascript so bear with me.

I have the following regular expression in Javascript:
commPattern = new RegExp("^\d\.\d{1,3}$") ;
to check for a format such as 0.01, 9.231, etc. however it doesn't work and returns null.
I tried doing an alert( commPattern ) and it displayed /^d.d{1,3}$/

I tried adding an extra "\" since I was out of ideas:
commPattern = new RegExp("^\\d\\.\\d{1,3}$") ;
and it worked! doing an alert( commPattern ) displays the regexp correctly: /^\d\.\d{1,3}$/.

This confuses me a lot since I've read in all the tutorials that "/" enables us to use stuff like "\d" for numeric characters or "/." to match ".". I'm thinking from what I've read, that I don't need to add the extra "\". I even tried my search string in and it was working fine.

Any help would be appreciated

Environment:
Windows 2000
IE 5.50.4807.2300 SP2- downloaded from I think, since MS stopped providing a 5.5 download.
 

Code:
commPattern = /^\d\.\d{1,3}$/;
alert( commPattern.test( '0.001' ) );

This work for ya?
 
yup, thanks theboyhope, forgot to follow up that i tested that syntax and it worked. I already figured out that "\" is also an escape character for strings in javascript, which is why the extra "\" is needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top