Bug Tracker

Opened 7 years ago

Closed 7 years ago

#12112 closed bug (cantfix)

RegExp.$1 being changed unexpectedly by jQuery selector parsing

Reported by: Ian Yang Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:

Description

After performing a regular expression such as:

if ('foo bar'.match(/(f\S+)/)) {
    // do something
}

We can utilize RegExp.$1 to output whatever is caught in the regular expression. For example:

if ('foo bar'.match(/(f\S+)/)) {
    alert(RegExp.$1); // alert "foo"
}

But if there is a certain jQuery selector parsing before the RegExp.$1, the RegExp.$1 might be changed unexpectedly. For example:

if ('foo bar'.match(/(f\S+)/)) {
    $('div').addClass(RegExp.$1); // the added class become "div" instead of "foo"
}

Change History (1)

comment:1 Changed 7 years ago by dmethvin

Resolution: cantfix
Status: newclosed

If you use RegExp global variables, you're gonna have a bad time. This isn't specific to jQuery or Sizzle, but a general problem with the RegExp globals and an excellent reason to avoid them. Any function you call can change them. They are not covered by the ECMA-262 standard, so you should not use them.

Note: See TracTickets for help on using tickets.