zoukankan      html  css  js  c++  java
  • Javascript正则表达入参是null

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

    var reg = /^[a-z0-9u4e00-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.
    
    
  • 相关阅读:
    fabrci网络调优
    fabric链码容器
    fabric文档查阅
    fabric基础设施管理-(五)移除网络
    fabric源码编译
    fabric网络状态监控
    fabric基础设施管理-(六)配置管理
    Scheme宏基础入门(转载)
    GO语言程序中解决中文日期格式的解析问题
    临别之际
  • 原文地址:https://www.cnblogs.com/ae6623/p/9170518.html
Copyright © 2011-2022 走看看