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

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.

Javascript正则表达入参是null的更多相关文章

  1. JavaScript 正则表达收集整理

    JavaScript 正则表达收集整理 //可为空 /^\s*$/ //密码验证,必须且只含有数字和字母,可以拥有英文符号,6-17位 /(?=.{,})(?=.*\d)(?=.*[a-z])[\x2 ...

  2. JavaScript正则表达

    Javascript 与正则表达式 一.正则表达式(regular expression简称res) 1.定义: 一个正则表达式就是由普通字符以及特殊字符(称为元字符)组成的文字模式.该模式描述在查找 ...

  3. JavaScript - 正则表达之二

    正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功:一旦有匹配不成功的字符则匹配失败. 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默 ...

  4. 【C语言】 strlen()入参空指针导致段错误

    背景: 在工作中调试sqlite3相关代码的时候,调用printf()打印sqlite3_exec()的执行日志:因为sqlite3_exec()保存日志的参数传入时为NULL,且没有执行错误,所以再 ...

  5. Javascript正则构造函数与正则表达字面量&&常用正则表达式

    本文不讨论正则表达式入门,即如何使用正则匹配.讨论的是两种创建正则表达式的优劣和一些细节,最后给出一些常用正则匹配表达式. Javascript中的正则表达式也是对象,我们可以使用两种方法创建正则表达 ...

  6. Egg.js 中入参的校验

    日常作业中免不了频繁处理 GET/POST 的入参,你当然可以每个 action 中都重复地去做这些事情, 从 query 或 body 取出入参, 对可选的入参进行判空, 处理入参的类型转换, 对入 ...

  7. Saiku根据入参日期查询出对应的数据(二十)

    Saiku根据入参日期查询出对应的数据 之前好像有写过一篇博客关于saiku date range的,现在进一步更新啦!!! 这里的日期筛选会更完善一些,需要提供两个参数 开始日期与结束日期(star ...

  8. js:正则表达

    一:正则表达对象方法 1:compile()方法   //编译正则表达式 实例:在字符串中全局搜索“man”,并用“person”替换,然后通过compile()方法,改变正则表达式,用person替 ...

  9. js的replace函数入参为function时的疑问

    近期在写js导出excel文件时运用到replace方法,此处详细的记录下它各个参数所代表的的意义. 定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式 ...

随机推荐

  1. 普林斯顿微积分读本 大纲与重点 (by zzd)

    普林斯顿微积分读本 大纲重点 由于博客园太菜,所以我用图片上传. 当前更新状态:未完待续,挖坑暂时不填了. UPD(2018-07-08): 稍微更一下,加一个本书的某一版本下载链接:https:// ...

  2. BZOJ1911 [Apio2010]特别行动队 - 动态规划 - 斜率优化

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 UPD(2018-04-01):用Latex重打了公式…… 题意概括 把一个整数序列划分成任意连续的段,使得划分出 ...

  3. Linux下java开发环境配置总结

    1 安装JDK,卸载以前的jdk,安装jdk1.8 : 参考:http://www.jb51.net/os/RedHat/73016.html 需要注意配置环境变量中的路径要和当前安装的jdk路径一致 ...

  4. Ajax技术使用补充

    Ajax技术使用补充 一.Ajax发送数据的几种形式 发送字符串或数字 $.ajax({ url:"/ajax_test.html/", type:'POST', data:{'v ...

  5. Django的请求生命周期

    Django的请求生命周期 请求生命周期 请求生命周期是指当用户在浏览器上输入url到用户看到网页的这个时间段内,Django后台所发生的事情. 1.客户端发送Http请求 2 .服务器接收,根据请求 ...

  6. Java高级面试题解析(一)

    最近,在看一些java高级面试题,我发现我在认真研究一个面试题的时候,我自己的收获是很大的,我们在看看面试题的时候,不仅仅要看这个问题本身,还要看这个问题的衍生问题,一个问题有些时候可能是一个问题群( ...

  7. 关于sql server的一种简单用法——在上面写查询语句,即可在下面修改数据

    选择数据库中的表,右键单击-->编辑前200行,然后在显示的页面中选择带SQL标志的图标

  8. 网络基础配置--usg系统升级

    1.usg2000系统升级 1.1.TFTP设置 这里用到一个工具:3CDeamon, 是在由3Com开发类别 Web Development Freeware 软件,是一个简易服务器工具,含TFTP ...

  9. LeetCode(12. 整数转罗马数字)

    问题描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II , ...

  10. 获取Android设备WIFI的MAC地址 “MAC地址”

    需要指出的是:wifi状态和wifi AP状态是互斥的状态:也就是一旦发现WIFI AP打开,WIFI是不能被打开的. 获取Android设备的WIFI MAC地址,首先需要将设备中的WIFI个人热点 ...