Closed Bug 322129 Opened 19 years ago Closed 19 years ago

Regular expression pattern /[]]/ not match right square bracket.

Categories

(Core :: JavaScript Engine, defect)

1.8 Branch
x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 272395

People

(Reporter: achowe, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.8) Gecko/20051111 Firefox/1.5
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.8) Gecko/20051111 Firefox/1.5

JavaScript regular expressions are based on Perl compatible regular expressions, which in turn are based on POSIX extended regular expressions. The pattern:

  /[]]/

should match a right square bracket in a string like "text [text] text". The pattern fails to match. See Single Unix Specification, chapter 9.3.5 RE Bracket Expression, point 1, paragraph 1:

http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_03_05

It would appear that the pattern:

  /[\]]/

will match a right square bracket, but is not standard nor historical practice with respect to regular expressions.

I found this while trying to perform a replacement to quote RE special characters:

  var quoted = string.replace(/([][{}().?/\+*^$|])/g, "\\$1");

The current workaround is to escape the right square bracket within character class:

  var quoted = string.replace(/([\][{}().?/\+*^$|])/g, "\\$1");


Reproducible: Always

Steps to Reproduce:
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Version: unspecified → 1.8 Branch

*** This bug has been marked as a duplicate of 272395 ***
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.