Wrox Programmer Forums
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 30th, 2005, 03:07 PM
Registered User
Join Date: Dec 2005
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,}/?

Old May 31st, 2006, 05:03 AM
ufo ufo is offline
Authorized User
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts

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.

Similar Threads
Thread Thread Starter Forum Replies Last Post
regex mrame XSLT 12 July 25th, 2008 09:37 AM
Working JavaScript Regex vinaura Javascript How-To 0 August 16th, 2007 01: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 09:21 AM
Regex Help boyer99g General .NET 2 October 8th, 2004 05:46 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.