为什么JavaScript RegExp / ^ \w + $ / match未定义? [英] Why does the JavaScript RegExp /^\w+$/ match undefined?
本文介绍了为什么JavaScript RegExp / ^ \w + $ / match未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么RegExp / ^ \w + $ /
匹配 undefined
?
示例代码:
alert(/ ^ \w + $ /。test(undefined) );
这将在Firefox 3中显示(只有浏览器测试过)。 b $ b
解决方案
当 undefined
被转换为字符串(正则表达式)时,字符串undefined
,然后匹配。
Why does the the RegExp /^\w+$/
match undefined
?
Example code:
alert(/^\w+$/.test(undefined));
This will display true in Firefox 3 (only browser I tested it on).
解决方案
When undefined
is cast to a string (which the regex does), it produces the string "undefined"
, which is then matched.
这篇关于为什么JavaScript RegExp / ^ \w + $ / match未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文