p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Regular Expressions
This is the forum to discuss the Wrox book Beginning Regular Expressions by Andrew Watt; ISBN: 9780764574894

Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Regular Expressions section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 30th, 2005, 03:07 PM
Registered User
 
Join Date: Dec 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default javascript regex

Can someone tell me why this simple test returns true?

var pattern = /\d{3}/;
var phoneNum = "85435";
var isValid = pattern.test(phoneNum);

I thought that this would require a 3 digit number only, so why is it
behaving like /\d{3,}/?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old May 31st, 2006, 06:03 AM
ufo ufo is offline
Authorized User
 
Join Date: Apr 2006
Location: , , .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi scottr,

patterns are supposed to go trough a whole text. often you will find yourself wanting several matches from the same document. For example you want to find all occurences off a misspelled word, to correct it. Basically, if you want to limit matching to a string of three digits only, you could do the following: /^\d{3}$/

If you only want to find 3 digit numbers in a longer text, use: /(?<!\d)\d{3}(?!\d)/

If they are just separated by whitespace, use /(?<=\s)\d{3}(?=\s)/

There is a quite good explanation about regex in Professional javascript for web developers by wrox.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
regex mrame XSLT 12 July 25th, 2008 10:37 AM
Working JavaScript Regex vinaura Javascript How-To 0 August 16th, 2007 02:06 PM
javascript 'new RegEx()' pint_of_best BOOK: Beginning Regular Expressions 1 November 30th, 2006 03:44 AM
Regex htran XSLT 2 May 18th, 2005 10:21 AM
Regex Help boyer99g General .NET 2 October 8th, 2004 06:46 PM



All times are GMT -4. The time now is 12:35 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc