/ regex / Literals总是RegExp对象吗? [英] Are /regex/ Literals always RegExp Objects?

查看:146
本文介绍了/ regex / Literals总是RegExp对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的问题是Javascript如何处理正则表达式文字。

Basically, my question is about how Javascript handles regex literals.

与数字,字符串和布尔形成对比,其中文字是原始数据类型,对应的数字,字符串和布尔值对象存在无缝类型转换,是正则表达式文字的RegExp对象的匿名实例,或者这是一个正则表达式被视为原始数据的情况,无缝类型转换为RegExp?

Contrasting with number, string and boolean where literals are primitive data types and corresponding Number, String and Boolean objects exist with seamless type conversion, are regex literals anonymous instances of the RegExp object or is this a case of regex being treated like primitive data with seamless type conversion to RegExp?

完整的参考Javascript,第2版,Powell和Schneider(MH)自相矛盾 - 在一个地方,作者说/ regex /会在需要时被自动转换为RegExp,在另一个地方他们说/ regex /只是一个RegExp的实例!

"The complete Reference Javascript, 2nd edition, Powell and Schneider (MH)" contradicts itself - at one place the authors say that /regex/ is automatically typecasted into RegExp when needed and at another place they say that /regex/ is nothing but an instance of RegExp!

编辑:请提供对可靠来源的引用

Please provide a reference to a reliable source

推荐答案

以下是规范的内容说:

正则表达式文字是一个输入元素,在扫描时转换为RegExp对象。在评估包含程序或函数开始之前创建对象。对文字的评价产生对该对象的引用;它不会创建新对象。程序中的两个正则表达式文字计算为正则表达式对象,即使两个文字的内容相同,也永远不会相互比较为 ===

A regular expression literal is an input element that is converted to a RegExp object when it is scanned. The object is created before evaluation of the containing program or function begins. Evaluation of the literal produces a reference to that object; it does not create a new object. Two regular expression literals in a program evaluate to regular expression objects that never compare as === to each other even if the two literals' contents are identical.

没有原始正则表达式类型以与 string 或<相同的方式自动装箱到对象code> number 。

There is no primitive regex type that autoboxes to an object in the same way as string or number.

但是,请注意,并非所有浏览器都实现每个文本实例化一次行为,包括 Safari 和IE6(以及可能更晚),因此便携式代码不应该依赖在上面。无效的ECMAScript 4草案会改变行为以匹配这些浏览器:

Note, however, that not all browsers implement the "instantiate-once-per-literal" behavior, including Safari and IE6 (and possibly later), so portable code shouldn't depend on it. The abortive ECMAScript 4 draft would have changed the behavior to match those browsers:


在ES3中,正则表达式字面值如 / a * b / mg 表示单个唯一在评估期间第一次遇到文字时创建的RegExp对象。在ES4中,每次在评估期间遇到文字时都会创建一个新的RegExp对象。

In ES3 a regular expression literal like /a*b/mg denotes a single unique RegExp object that is created the first time the literal is encountered during evaluation. In ES4 a new RegExp object is created every time the literal is encountered during evaluation.

此外,某些浏览器( Firefox< 3 Safari )报告 typeof / regex / as function,因此便携式代码应避免在RegExp实例上使用 typeof - 坚持使用 instanceof

Also, some browsers (Firefox <3, Safari) report typeof /regex/ as "function", so portable code should avoid typeof on RegExp instances—stick with instanceof.

这篇关于/ regex / Literals总是RegExp对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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