正则表达式的 typeof
有没有办法检测 JavaScript 对象是否是正则表达式?
例如,我想做这样的事情:
var t = /^foo(bar)?$/i;
alert(typeof t); //I want this to return "regexp"
这可能吗?
谢谢!
编辑:感谢您的所有答案。看来我有两个非常好的选择:
obj.constructor.name === "RegExp"
或者
obj instanceof RegExp
这两种方法有什么主要优点/缺点吗?
再次感谢!
Is there anyway to detect if a JavaScript object is a regex?
For example, I would like to do something like this:
var t = /^foo(bar)?$/i;
alert(typeof t); //I want this to return "regexp"
Is this possible?
Thanks!
EDIT: Thanks for all the answers. It seems I have two very good choices:
obj.constructor.name === "RegExp"
or
obj instanceof RegExp
Any major pros/cons to either method?
Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 instanceof 运算符:
事实上,这几乎与以下内容相同:
请记住,as RegExp 不是 原始数据类型,无法使用
typeof
运算符,这可能是最好的选择对于这个问题。但是您可以使用上面的这个技巧或其他技巧,例如鸭子类型检查例如,检查此类对象是否具有任何重要的方法或属性,或者通过其内部类值(通过使用
{}.toString.call(instaceOfMyObject)
)。You can use instanceof operator:
In fact, that is almost the same as:
Keep in mind that as RegExp is not a primitive data type, it is not possible to use
typeof
operator which could be the best option for this question.But you can use this trick above or others like duck type checking, for example, checking if such object has any vital methods or properties, or by its internal class value (by using
{}.toString.call(instaceOfMyObject)
).这是规范中提到的获取对象类的方式。
来自ECMAScript 5,第 8.6.2 节对象内部属性和方法:
RegExp 是规范中第 15.10 节 RegExp(RegularExpression)Objects 中定义的一类对象:
This is the way mentioned in the specification for getting the class of object.
From ECMAScript 5, Section 8.6.2 Object Internal Properties and Methods:
A RegExp is a class of object defined in the spec at Section 15.10 RegExp(RegularExpression)Objects:
尝试一下
.constructor
属性:Give the
.constructor
property a whirl:来自 underscore.js
From underscore.js
在谷歌浏览器中工作:
Works in google chrome:
“Regexp”不是原生 Javascript 类型。上面的大多数答案告诉您如何完成任务,但没有告诉您原因。这是原因。
"Regexp" is not a native Javascript type. Most of the above answers tell you how to accomplish your task, but not why. Here's why.
没有绝对的方法来检查这一点,到目前为止最好的答案是,
但这种方法有一个缺点,那就是如果正则表达式对象来自另一个窗口,它将返回 false。
There is no absolute way of checking this, so far the best answer is
but there is one down side to this approach and that's it will return false if the regular expression object is commeing from an other window.
这里有两种方法:
如果字符串文字由正则表达式反斜杠分隔符分隔,则正则表达式自检将失败。
如果
Object.seal
或Object.freeze
在用户定义的对象上运行,并且该对象也具有上述所有属性,则delete
> 语句将返回误报。参考
ECMAScript 5,第 15.10 节。 4.1 新的 RegExp(pattern, flags)
when 的一些用例是什么
delete
在 JavaScript 中非常有用?为什么这个可配置属性不可删除?
Webkit JavaScriptCore 源代码:ObjectConstructor.cpp
JavaScript 中的对象属性
Here are two ways:
If a string literal is delimited by the regexp backslash delimiter, the regexp self test will fail.
If
Object.seal
orObject.freeze
are run on a user-defined object, and that object also has all of the aforementioned properties, thedelete
statement will return a false positive.References
ECMAScript 5, Section 15.10.4.1 new RegExp(pattern, flags)
What are some use-cases of when
delete
has been very useful in JavaScript?Why is this configurable property not deletable?
Webkit JavaScriptCore source: ObjectConstructor.cpp
Object Properties in JavaScript