admin管理员组文章数量:1244222
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?
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?
Share Improve this question edited Jun 19, 2014 at 15:27 Eamon Nerbonne 48.1k20 gold badges104 silver badges171 bronze badges asked Jan 11, 2009 at 12:25 nickfnickf 546k198 gold badges658 silver badges726 bronze badges 5- 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 Commented Jan 11, 2009 at 12:34
- I think at least a link to the previous almost-the-same question should be provided: stackoverflow./questions/432493/… – Rene Saarsoo Commented Jan 11, 2009 at 12:57
- @PEZ: what literal matching syntax are you talking about? – nickf Commented Jan 11, 2009 at 14:12
- 1 @PEZ: developer.mozilla/en-US/docs/JavaScript/Guide/… – Robert Commented Oct 31, 2012 at 21:19
- This is not a firefox issue, it's a firebug issue. – Eamon Nerbonne Commented Jun 19, 2014 at 15:27
2 Answers
Reset to default 16This 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.
Seems like %A
somehow translates into the string undefined
.
Try escaping the %A
part, I think that will solve the problem.
本文标签: regexWhy is a matched substring returning quotundefinedquot in JavaScriptStack Overflow
版权声明:本文标题:regex - Why is a matched substring returning "undefined" in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740183740a2237820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论