Javascript 替换空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16303899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:08:20  来源:igfitidea点击:

Javascript replace null

javascript

提问by petebowden

var p = "null"
var q = null;
(p == q) //false. as Expected.

p.replace(null, "replaced") // outputs replaced. Not expected.
p.replace("null", "replaced") //outputs replaced. Expected.

q.replace(null, "replaced") // error.  Expected.
q.replace("null", "replaced") //error. Expected.

Why? Does replace not differentiate between "null"and null?

为什么?替换不区分"null"null吗?

I ask because I ran into a bug in angularjs:

我问是因为我在 angularjs 中遇到了一个错误:

replace((pctEncodeSpaces ? null : /%20/g), '+');

If for example, someone had a username of "null"and it was used as url, it would be replaced with "+" on any $httpcalls. e.g. GET /user/null.

例如,如果某人的用户名是"null"并且它被用作 url,那么在任何$http调用中它都会被替换为“+” 。例如GET /user/null

Not that this scenario would occur often, but I'm more curious why replace treats nulland "null"as the same thing. Does replace do a .tostring on nullbefore it does the replacement? Is this just a quirk of Javascript?

并不是说这种情况会经常发生,但我更好奇为什么要替换零食null"null"作为同一件事。null在替换之前替换是否执行 .tostring ?这只是 Javascript 的一个怪癖吗?

I verified this on both IE and Chrome's implementations of replace.

我在 IE 和 Chrome 的replace.

采纳答案by Russ Cam

Yes, this is expected according to the spec for replace(bolded relevant line, or page 146 of the ECMA-262 final draft). The first argument is checked to see if it is a regex and if not, it has toString()called on it (well, converted to a string somehow).

是的,根据规范replace(粗体相关行,或ECMA-262 最终草案的第 146 页),这是预期的。检查第一个参数以查看它是否是正则表达式,如果不是,则toString()调用它(好吧,以某种方式转换为字符串)。

15.5.4.11 String.prototype.replace(searchValue, replaceValue)

Let string denote the result of converting the this value to a string.

Cut for brevity

IfsearchValueis not a regular expression, let searchStringbe ToString(searchValue)and search string for the first occurrence of searchString. Let m be 0.

Cut for brevity

15.5.4.11 String.prototype.replace(searchValue, replaceValue)

让 string 表示将 this 值转换为字符串的结果。

为简洁起见

如果searchValue不是正则表达式,则searchStringbeToString(searchValue)并搜索字符串以查找第一次出现的 searchString。让 m 为 0。

为简洁起见

回答by Alnitak

In the ES5 specification for String.prototype.replace:

ES5 规范中String.prototype.replace

15.5.4.11 String.prototype.replace (searchValue, replaceValue)

...

If searchValueis not a regular expression, let searchStringbe ToString(searchValue)and search stringfor the first occurrence of searchString

15.5.4.11 String.prototype.replace (searchValue, replaceValue)

...

如果searchValue不是正则表达式,让searchStringbeToString(searchValue)并搜索string第一次出现的searchString

So, "".replace(null, XXX)will indeed convert the nullto the string "null"

所以,"".replace(null, XXX)确实会转换null为字符串"null"

Note that ToString()does notmean null.toString()- it's an internal defined operation within the JavaScript interpreter.

请注意,ToString()并不意味着null.toString()- 它是 JavaScript 解释器中内部定义的操作。

回答by Paul S.

"null".replace(null, "replaced") // outputs replaced. Not expected.

Does replace not differentiate between "null" and null

不替换不区分“空”和空

This is because parameter 1 of replaceis converted to String.

这是因为参数 1replace被转换为String

''+null === "null"; // true by cast to string

Furthermore, as replacetakes RegExpobjects, you can also think about how

此外,由于replace采用的RegExp对象,你也可以想想如何

RegExp(null).toString() === "/null/"; // true

回答by brunoais

For the not expected one, there's a simple answer. The null is casted to string by the replace() method.
So that is also an expected action

对于意想不到的人,有一个简单的答案。通过 replace() 方法将空值转换为字符串。
所以这也是一个预期的动作