Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Regex Help

  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regex Help

    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

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    What is
    Code:
    \d{1,2,3}
    suppose to do?
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #3
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm, 3 decimal places??
    what i'd like is any number of any size with the option of decimal to 3 places.

  • #4
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    {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.
    Code:
    /\d+\.(?=\d{1,3})/
    Last edited by hemebond; 08-19-2005 at 03:05 AM.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #5
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How would I make the decimal optional?

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

  • #6
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think this is closer to what you're looking for:
    Code:
    /\d+(?:\.\d{1,3})?/

  • #7
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by facets
    How would I make the decimal optional?

    Code:
    /\d+\.(?=\d{1,3})/
    It is optional (I think) and is only matched if followed by more numbers.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #8
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    (?=) is a lookahead and isn't included in the match. When it's used like this:
    Code:
    /\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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •