Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-25-2005, 02:24 AM   PM User | #1
DHTML Kitchen
Regular Coder

 
Join Date: Mar 2004
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
DHTML Kitchen is an unknown quantity at this point
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.
__________________
http://dhtmlkitchen.com/
DHTML Kitchen is offline   Reply With Quote
Old 11-25-2005, 07:02 AM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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
liorean is offline   Reply With Quote
Old 11-25-2005, 07:42 AM   PM User | #3
DHTML Kitchen
Regular Coder

 
Join Date: Mar 2004
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
DHTML Kitchen is an unknown quantity at this point
Good point!

Quote:
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
__________________
http://dhtmlkitchen.com/
DHTML Kitchen is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:06 AM.


Advertisement
Log in to turn off these ads.