Javascript 正则表达式原型 [英] Javascript Regex prototype

查看:62
本文介绍了Javascript 正则表达式原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Chrome 的控制台为 RegExp 的原型显示 /(?:)/?

Why is Chrome's console displaying /(?:)/ for the RegExp's prototype?

console.log(RegExp.prototype);
console.log(/a/.__proto__);

这是特定于实现的东西吗?IE 正在显示 //.

Is this something implementation specific? IE is displaying //.

这只是一个出于好奇的问题.当我遇到这个时,我试图在javascript中查看正则表达式的所有原型方法.当然我找到了其他方法来查找这些方法,但这让我想知道为什么会显示这个结果.

This is just a question out of curiousity. I tried to view all prototype methods of regular expressions in javascript, when I encountered this. Of course I found other ways of looking up those methods, but this keeps me wondering why this result is displayed.

我希望这个问题以前没有被问过 - 在 Stackoverflow 上没有找到任何东西.谢谢!

I hope this question has not been asked before - did not find anything on Stackoverflow. Thanks!

推荐答案

规范中的相关部分

RegExp 原型对象本身就是一个正则表达式对象;它的[[Class]] 是RegExp".RegExp 原型的初始值对象的数据属性 (15.10.7) 被设置为好像对象是由表达式 new RegExp() 创建,其中 RegExp 是该标准具有该名称的内置构造函数.

The RegExp prototype object is itself a regular expression object; its [[Class]] is "RegExp". The initial values of the RegExp prototype object’s data properties (15.10.7) are set as if the object was created by the expression new RegExp() where RegExp is that standard built-in constructor with that name.

然后我们去new RegExp() 部分,这与使用空字符串调用它相同.所以现在我们有空的正则表达式对象作为原型.

Then we go to new RegExp() section, which is same as calling it with the empty string. So now we have empty regex object as the prototype.

字符串表示定义为:

返回由字符串/"连接而成的字符串值,此 RegExp 对象的源属性的字符串值,和/";如果全局属性为真,则加g",如果忽略大小写,则加i"属性为真,如果多行属性为真,则为m".

Return the String value formed by concatenating the Strings "/", the String value of the source property of this RegExp object, and "/"; plus "g" if the global property is true, "i" if the ignoreCase property is true, and "m" if the multiline property is true.

注意返回的字符串具有RegularExpressionLiteral的形式计算为另一个具有相同行为的 RegExp 对象这个对象.

NOTE The returned String has the form of a RegularExpressionLiteral that evaluates to another RegExp object with the same behaviour as this object.

它最终归结为 .source 属性(在 新的 RegExp 部分),只要它在用作 regex 时表现相同,它就是实现定义的:

It ultimately comes down to .source property (explained in the new RegExp section), which is implementation defined as long as it behaves the same when used as regex:

设 S 是一个等效于 P 的 Pattern 形式的字符串,其中某些字符被转义如下所述.S可能是也可能不是与 P 或图案相同;然而,内部程序将将 S 评估为模式的结果必须与由构造对象的 [[Match]] 给出的内部过程内部财产.

Let S be a String in the form of a Pattern equivalent to P, in which certain characters are escaped as described below. S may or may not be identical to P or pattern; however, the internal procedure that would result from evaluating S as a Pattern must behave identically to the internal procedure given by the constructed object's [[Match]] internal property.

由于 new RegExp("(?:)")new RegExp("") 的行为相同,所以 chrome 和 IE 都正确地遵循规范.该规范甚至以这种特定情况为例:

Since new RegExp("(?:)") and new RegExp("") behave identically, both chrome and IE are following the spec correctly. The spec even mentions this specific situation as an example:

如果 P 是空字符串,则可以通过让 S是(?:)"

If P is the empty String, this specification can be met by letting S be "(?:)"

Chrome 的方式可以被认为是更好的",因为它似乎也可以用作正则表达式文字:/(?:)/ 是有效的正则表达式文字,而 //开始一行注释.

Chrome's way can be considered "better" since it appears to work as regexp literal as well: /(?:)/ is a valid regex literal whereas // starts a one-line comment.

这篇关于Javascript 正则表达式原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆