RegExp not matching with style backgroundColor (rgb(x, x, x))

CLOSED INVALID

Status

()

Core
JavaScript Engine
CLOSED INVALID
16 years ago
16 years ago

People

(Reporter: Herbert Georg Fischer, Assigned: rogerl (gone))

Tracking

Trunk
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(URL)

(Reporter)

Description

16 years ago
User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030112
Build Identifier: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030112

I've created a regular expression to test and separate parts from 
a string containing rgb info from a div.style.backgroundColor property, but It's
not working.

I've already successfuly tested with Perl and PHP the same RegExp and it worked.
Basicaly, I just want to get each part of the RGB triplet to do some math. Since
on some browsers (like I.E.) I got a string on the form "#XXXXXX" and on others
(Mozilla) I got a string on the form "rgb(XXX,XXX,XXX)", I need to test which
I'm having on the browser and split it on RGB parts.

Reproducible: Always

Steps to Reproduce:
1. Open http://pontoip.com.br/3dicon/colorselector.php
2. Click on one of the 3 color bars

Actual Results:  
A result with "Not match" with show up inside an alert box, since there in no
match with the string with the regexp.

Expected Results:  
Match the string and split it up and result an array with values.

The first part of the code, that was made for browsers that use "#XXXXXX" for
color styles, is already working.
  var RGBExtended = new RegExp("rgb\((\d{1,3}),\s(\d{1,3}),\s(\d{1,3})\)", "i");

should be

  var RGBExtended = new
RegExp("rgb\\((\\d{1,3}),\\s(\\d{1,3}),\\s(\\d{1,3})\\)", "i");

since '\' is an escape char in JS strings.
Status: UNCONFIRMED → RESOLVED
Last Resolved: 16 years ago
Resolution: --- → INVALID

Comment 2

16 years ago
Boris is right. Marking Verified. Here is a session from the
standalone JS shell. Watch what happens to the "\" characters:

js> new RegExp("rgb\((\d{1,3}),\s(\d{1,3}),\s(\d{1,3})\)", "i");
/rgb((d{1,3}),s(d{1,3}),s(d{1,3}))/i


The "\" characters disappear not because of anything that RegExp does.
They are treated as escape characters inside any JavaScript string:

js> new String("rgb\((\d{1,3}),\s(\d{1,3}),\s(\d{1,3})\)", "i");
rgb((d{1,3}),s(d{1,3}),s(d{1,3}))


Because of this issue, many coders prefer not to create regexps
via the RegExp constructor (which takes a string as its argument).
Instead, you can use a regexp literal, as follows:

js> RGBExtended = /rgb\((\d{1,3}),\s(\d{1,3}),\s(\d{1,3})\)/i
/rgb\((\d{1,3}),\s(\d{1,3}),\s(\d{1,3})\)/i


Here, all the backslash characters "\" are preserved, because
they are inside a regexp literal instead of a string literal.

Notice that regexp literals are delimited by forward-slash
characters, not double-quotes. And the i flag is put at the end
(again, without quotes).
Status: RESOLVED → VERIFIED

Comment 3

16 years ago
Another point: I notice at the site, you have

  <script language="JavaScript1.4" type="text/javascript">

Why language="JavaScript1.4"? The language is up to version 1.5
already. Unless you have a specific reason to use an earlier 
version, you should always leave the version out, as in:

  <script language="JavaScript" type="text/javascript">

This will allow the browser to use the latest version available -
(Reporter)

Comment 4

16 years ago
It's not a bug, but something bad documented.
All RegExp references I saw on the web, does not mention that, about escaping
chars when using RegExp class.
Severity: major → normal
Status: VERIFIED → CLOSED
Summary: RexExp not matching with style backgroundColor (rgb(x, x, x)) → RegExp not matching with style backgroundColor (rgb(x, x, x))
You need to log in before you can comment on or make changes to this bug.