0

所以我在调试工作中的一些奇怪事件时偶然发现了这一点,其中字符串在文本中包含“null”:

"Some string containing testnull in the text".replace(null, "REPLACER")
"> Some string containing testREPLACER in the text"

尽管

"Some string containing testnull in the text".replace(undefined, "REPLACER")
"> Some string containing testnull in the text"
4

2 回答 2

4

查看文档replace

句法

 const newStr = str.replace(regexp|substr, newSubstr|function)

regexp (pattern)
RegExp 对象或文字。匹配项或匹配项被替换为 newSubstr 或指定函数返回的值。

substr
要被 newSubstr 替换的字符串。它被视为文字字符串,不会被解释为正则表达式。只有第一个匹配项将被替换。

第一个参数必须是正则表达式或字符串。

null也不是,所以它是自动类型转换的。如果你转换null成一个字符串,你会得到"null".

const a = "" + null;
const b = "null";

console.log(a === b);

于 2020-05-27T08:56:53.530 回答
-1
于 2020-05-27T17:09:20.407 回答