172

有没有办法检测一个 JavaScript 对象是否是一个正则表达式?

例如,我想做这样的事情:

var t = /^foo(bar)?$/i;
alert(typeof t); //I want this to return "regexp"

这可能吗?

谢谢!

编辑:感谢所有的答案。看来我有两个非常好的选择:

obj.constructor.name === "RegExp"

或者

obj instanceof RegExp

任何一种方法的主要优点/缺点?

再次感谢!

4

9 回答 9

234

您可以使用instanceof运算符:

var t = /^foo(bar)?$/i;
alert(t instanceof RegExp);//returns true

实际上,这几乎与以下内容相同:

var t = /^foo(bar)?$/i;
alert(t.constructor == RegExp);//returns true

请记住,由于RegExp不是原始数据类型,因此无法使用typeof运算符,这可能是该问题的最佳选择

但是您可以使用上面的这个技巧或其他方法,例如鸭子类型检查,例如,检查此类对象是否具有任何重要的方法或属性,或者通过其内部类值(通过 using {}.toString.call(instaceOfMyObject))。

于 2010-12-02T20:08:59.517 回答
24
alert( Object.prototype.toString.call( t ) ); // [object RegExp]

这是规范中提到的获取对象类的方式。

ECMAScript 5,第 8.6.2 节对象内部属性和方法

[[Class]] 内部属性的值由本规范为每种内置对象定义。宿主对象的 [[Class]] 内部属性的值可以是除“Arguments”、“Array”、“Boolean”、“Date”、“Error”、“Function”、“JSON”之一之外的任何 String 值、“数学”、“数字”、“对象”、“正则表达式”和“字符串”。[[Class]] 内部属性的值在内部用于区分不同种类的对象。请注意,除了通过 Object.prototype.toString(参见 15.2.4.2)外,本规范没有为程序提供任何访问该值的方法。

RegExp 是在第 15.10 节 RegExp(RegularExpression)Objects规范中定义的一类对象:

RegExp 对象包含一个正则表达式和相关的标志。

于 2010-12-02T20:13:20.577 回答
14

试一试.constructor房产:

> /^foo(bar)?$/i.constructor
function RegExp() { [native code] }
> /^foo(bar)?$/i.constructor.name
"RegExp"
> /^foo(bar)?$/i.constructor == RegExp
true
于 2010-12-02T20:05:15.463 回答
10

来自underscore.js

// Is the given value a regular expression?
  _.isRegExp = function(obj) {
    return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
  };
于 2010-12-02T20:05:23.360 回答
3

在谷歌浏览器中工作:

x = /^foo(bar)?$/i;
x == RegExp(x); // true
y = "hello";
y == RegExp(y); // false
于 2010-12-02T20:04:53.877 回答
0

“正则表达式”不是原生 Javascript 类型。上面的大多数答案告诉你如何完成你的任务,但没有告诉你为什么。这就是为什么

于 2010-12-02T20:06:55.467 回答
0

没有绝对的方法来检查这个,到目前为止最好的答案是

var t = /^foo(bar)?$/i;
alert(t instanceof RegExp);//returns true

但是这种方法有一个缺点,如果正则表达式对象来自另一个窗口,它将返回 false。

于 2013-10-17T10:57:25.610 回答
0

这里有两种方法:

/^\/.*\/$/.test(/hi/) /* test regexp literal via regexp literal */
/^\/.*\/$/.test(RegExp("hi") ) /* test RegExp constructor via regexp literal */
RegExp("^/" + ".*" + "/$").test(/hi/) /* test regexp literal via RegExp constructor */
RegExp("^/" + ".*" + "/$").test(RegExp("hi") ) /* test RegExp constructor via RegExp constructor */ 

delete RegExp("hi").source /* test via deletion of the source property */
delete /hi/.global /* test via deletion of the global property */
delete /hi/.ignoreCase /* test via deletion of the ignoreCase property */
delete RegExp("hi").multiline /* test via deletion of the multiline property */
delete RegExp("hi").lastIndex /* test via deletion of the lastIndex property */

如果字符串文字由正则表达式反斜杠分隔符分隔,则正则表达式自检将失败。

如果Object.sealObject.freeze在用户定义的对象上运行,并且该对象还具有上述所有属性,则该delete语句将返回误报。

参考

于 2014-07-03T21:50:28.623 回答
0

我一直在寻找,typeof regex因为我尝试将它type definition用于函数上的 TypeScript 道具。

然后我做下一个:

const RegexType = /$/;

type paramProps = {
  regexParam: typeof RegexType;
}

你可以在这里测试:

const RegexType = /$/;
const t = /^foo(bar)?$/i;

console.log(typeof t == typeof RegexType) //true

于 2022-01-27T16:08:59.840 回答