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.
    
    
  • 相关阅读:
    Git 分支开发规范
    小程序技术调研
    弹性布局
    vue 自定义指令的魅力
    记一次基于 mpvue 的小程序开发及上线实战
    mpvue学习笔记-之微信小程序数据请求封装
    微信小程序开发框架 Wepy 的使用
    Hbuilder 开发微信小程序的代码高亮
    luogu3807 【模板】 卢卡斯定理
    luogu1955 [NOI2015] 程序自动分析
  • 原文地址:https://www.cnblogs.com/ae6623/p/9170518.html
Copyright © 2011-2022 走看看