Javascript正则表达入参是null

今天群友问了一个问题,如下的执行结果是什么?

var reg = /^[a-z0-9\u4e00-\u9fa5]{0,15}$/;
console.log(reg.test(null)); // true

一般来说,正则是只匹配字符串的,但是楼上这段代码,也没有报错,于是翻了翻Es5规范,于是找到了答案,正则在执行test的时候,会优先调用 toString 方法,于是 null -> 'null'

详见 http://es5.github.io/#x15.10.6.3

15.10.6.2 RegExp.prototype.exec(string) # Ⓣ ① Ⓡ
Performs a regular expression match of string against the regular expression and returns an Array object containing the results of the match, or null if string did not match.

The String ToString(string) is searched for an occurrence of the regular expression pattern as follows:

Let R be this RegExp object.

Let S be the value of ToString(string).

Let length be the length of S.

Let lastIndex be the result of calling the [[Get]] internal method of R with argument "lastIndex"..

Let i be the value of ToInteger(lastIndex).

Let global be the result of calling the [[Get]] internal method of R with argument "global".

If global is false, then let i = 0.

Let matchSucceeded be false.

Repeat, while matchSucceeded is false

If i < 0 or i > length, then

Call the [[Put]] internal method of R with arguments "lastIndex", 0, and true.

Return null.

Call the [[Match]] internal method of R with arguments S and i.

If [[Match]] returned failure, then

Let i = i+1.

else

Let r be the State result of the call to [[Match]].

Set matchSucceeded to true.

Let e be r's endIndex value.

If global is true,

Call the [[Put]] internal method of R with arguments "lastIndex", e, and true.

Let n be the length of r's captures array. (This is the same value as 15.10.2.1's NCapturingParens.)

Let A be a new array created as if by the expression new Array() where Array is the standard built-in constructor with that name.

Let matchIndex be the position of the matched substring within the complete String S.

Call the [[DefineOwnProperty]] internal method of A with arguments "index", Property Descriptor {[[Value]]: matchIndex, [[Writable]: true, [[Enumerable]]: true, [[Configurable]]: true}, and true.

Call the [[DefineOwnProperty]] internal method of A with arguments "input", Property Descriptor {[[Value]]: S, [[Writable]: true, [[Enumerable]]: true, [[Configurable]]: true}, and true.

Call the [[DefineOwnProperty]] internal method of A with arguments "length", Property Descriptor {[[Value]]: n + 1}, and true.

Let matchedSubstr be the matched substring (i.e. the portion of S between offset i inclusive and offset e exclusive).

Call the [[DefineOwnProperty]] internal method of A with arguments "0", Property Descriptor {[[Value]]: matchedSubstr, [[Writable]: true, [[Enumerable]]: true, [[Configurable]]: true}, and true.

For each integer i such that I > 0 and I ≤ n

Let captureI be ith element of r's captures array.

Call the [[DefineOwnProperty]] internal method of A with arguments ToString(i), Property Descriptor {[[Value]]: captureI, [[Writable]: true, [[Enumerable]]: true, [[Configurable]]: true}, and true.

Return A.

15.10.6.3 RegExp.prototype.test(string) # Ⓣ Ⓡ
The following steps are taken:

Let match be the result of evaluating the RegExp.prototype.exec (15.10.6.2) algorithm upon this RegExp object using string as the argument.

If match is not null, then return true; else return false.

转载于:https://www.cnblogs.com/ae6623/p/9170518.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值