javascript - 为什么 String.prototype.match() 返回 null 而不是空数组?
问题描述
示例取自MDN webdocs
var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /[A-Z]/g;
var found = paragraph.match(regex);
console.log(found); // will return an array of matches and returns null when nothing matches.
想知道在没有匹配项时返回 null 而不是 Empty Array 背后的原因吗?
解决方案
这就是String.prototype.match
EcmaScript 标准中的定义
- 21.1.3.11String.prototype.match(正则表达式)
- 21.2.5.7RegExp.prototype [@@match](字符串)
- 21.2.5.2.2运行时语义:RegExpBuiltinExec(R,S)
简而言之:如果没有匹配项 - 它null
按标准返回。
推荐阅读
- java - 为什么我无法在资产文件夹中打开此文件?
- c# - 如何将面板设置为统一实例化预制件的父级?
- c++ - 在cpp中将十六进制字符串转换为字符数组
- python - 是否有可以处理数组输入的python阶乘函数?
- c++ - 如何在 centos 中使用更新选项?
- python - 累计配电线路
- excel-formula - 间接公式 = 间接(H9&"1:1")
- reactjs - 如何使用 Cookie 进行存储 jwt 令牌用户认证?
- php - 如何在 WooCommerce 的结帐页面上禁用更改运输方式的功能
- javascript - Cordova "Origin: file://"-Error using and Android 10, old versions working