5

I came across a strange behaviour when doing some regular expressions in JavaScript today (Firefox 3 on Windows Vista).

var str = "format_%A";
var format = /(?:^|\s)format_(.*?)(?:\s|$)/.exec(str);

console.log(format);    // ["format_%A", "%A"]
console.log(format[0]); // "format_undefined"
console.log(format[1]); // Undefined

There's nothing wrong with the regular expression. As you can see, it has matched the correct part in the first console.log call.

Internet Explorer 7 and Chrome both behave as expected: format[1] returns "%A" (well, Internet Explorer 7 doing something right was a bit unexpected...)

Is this a bug in Firefox, or some "feature" I don't know about?

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
nickf
  • 537,072
  • 198
  • 649
  • 721
  • I've never seen the literal matching syntax you're using here. Can you point at some web resource where one can read about it? – PEZ Jan 11 '09 at 12:34
  • I think at least a link to the previous almost-the-same question should be provided: http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex – Rene Saarsoo Jan 11 '09 at 12:57
  • @PEZ: what literal matching syntax are you talking about? – nickf Jan 11 '09 at 14:12
  • 1
    @PEZ: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions – Robert Oct 31 '12 at 21:19
  • This is not a firefox issue, it's a firebug issue. – Eamon Nerbonne Jun 19 '14 at 15:27

2 Answers2

16

This is because console.log() works like printf(). The first argument to console.log() is actually a format string which may be followed with additional arguments. %A is a placeholder. For example:

console.log("My name is %A", "John"); // My name is "John"

See console.log() documentation for details. %A and any other undocumented placeholders seem to do the same as %o.

Rene Saarsoo
  • 13,580
  • 8
  • 57
  • 85
1

Seems like %A somehow translates into the string undefined.

Try escaping the %A part, I think that will solve the problem.

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395