Skip to main content

Bug Tracker

Side navigation

#12112 closed bug (cantfix)

Opened July 20, 2012 10:28AM UTC

Closed July 20, 2012 12:52PM UTC

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"
}
Attachments (0)
Change History (1)

Changed July 20, 2012 12:52PM UTC by dmethvin comment:1

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.