...

View Full Version : Regex Help



facets
08-19-2005, 01:37 AM
Hi All,

I'm getting the following error from my regex code.
Can anyone help me debug it?

Error: invalid quantifier {
Source File: http://xx.xx.xx.xx/includes/validation.js
Line: 199, Column: 23
Source Code:
var thicknessRegxp = /^[0-9]\d{0,2}(\.\d{1,2,3})?%?$/;

tia, will

hemebond
08-19-2005, 02:02 AM
What is
\d{1,2,3}suppose to do?

facets
08-19-2005, 02:09 AM
hmm, 3 decimal places??
what i'd like is any number of any size with the option of decimal to 3 places.

hemebond
08-19-2005, 02:33 AM
{n}
Where n is a positive integer. Matches exactly n occurrences of the preceding item.

For example, /a{2}/ doesn't match the 'a' in "candy," but it matches all of the a's in "caandy," and the first two a's in "caaandy."

{n,}
Where n is a positive integer. Matches at least n occurrences of the preceding item.

For example, /a{2,} doesn't match the 'a' in "candy", but matches all of the a's in "caandy" and in "caaaaaaandy."

{n,m}
Where n and m are positive integers. Matches at least n and at most m occurrences of the preceding item.

For example, /a{1,3}/ matches nothing in "cndy", the 'a' in "candy," the first two a's in "caandy," and the first three a's in "caaaaaaandy". Notice that when matching "caaaaaaandy", the match is "aaa", even though the original string had more a's in it.
/\d+\.(?=\d{1,3})/

facets
08-19-2005, 06:45 AM
How would I make the decimal optional?


/\d+\.(?=\d{1,3})/

Mongus
08-19-2005, 07:56 AM
I think this is closer to what you're looking for:


/\d+(?:\.\d{1,3})?/

hemebond
08-19-2005, 08:35 AM
How would I make the decimal optional?


/\d+\.(?=\d{1,3})/It is optional (I think) and is only matched if followed by more numbers.

Mongus
08-19-2005, 08:49 AM
(?=) is a lookahead and isn't included in the match. When it's used like this:


/\d+\.(?=\d{1,3})/
it would match numbers followed by a period only if they were followed by 1-3 numbers. It wouldn't match numbers without a period and any matches it did find wouldn't include the numbers following the period.

Try the regex I posted above. I think it'll work for you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum