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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can this RegExp be Optimized?

    Code:
    var COLOR_PATTERN = /#[0-9a-z]{3,6}|rgb\(\s*(?:[\d]{1,3})(?:%)?\s*\,\s*(?:[\d]{1,3})(?:%)?\s*\,\s*(?:[\d]{1,3})(?:%)?\s*\)/i;
    (IE doesn't support const kwd and I'm tryina support IE).


    Here's a breakdown:
    Code:
    #[0-9a-f]{3,6}                  - matches #333 or #ff0044
    rgb\(                           - matches rgb(
    
    \s*(?:[\d]{1,3})(?:%)?\s*       - matches 122 or 122%  -- call this nPattern.
    \,                              - matches a comma
    \s*(?:[\d]{1,3})(?:%)?\s*\      -(SAME AS nPattern)
    \,                              - matches a comma
    \s*(?:[\d]{1,3})(?:%)?\s*       - (SAME AS nPattern)
    \)/i;
    My question is regarding the simplification of nPattern. I repeat the same thing three times. Can I store and backreference within the same pattern? I don't know if it's possible.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Backreferences store the matched text, not the matching patterns, so no, you can't.

    However, I think rgb values either have to be all unitless or all per cent, I don't think you're allowed to have a mix. (not sure about this). This means you could capture one or zero per cent characters for red and backreference that for green and blue.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good point!

    The format of an RGB value in the functional notation is 'rgb(' followed by a comma-separated list of three numerical values (either three integer values or three percentage values) followed by ')'. The integer value 255 corresponds to 100%, and to F or FF in the hexadecimal notation: rgb(255,255,255) = rgb(100%,100%,100%) = #FFF. Whitespace characters are allowed around the numerical values.
    My gf is pissed at me for ignoring her on thanksgiving. time ta go! Thanks though


  •  

    Posting Permissions

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