te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Javascript replace null - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript replace null - Stack Overflow

programmeradmin2浏览0评论
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?

I ask because I ran into a bug in 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 $http calls. e.g. GET /user/null.

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

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

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?

I ask because I ran into a bug in 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 $http calls. e.g. GET /user/null.

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

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

Share Improve this question edited Apr 30, 2013 at 15:48 Paul S. 66.4k9 gold badges128 silver badges143 bronze badges asked Apr 30, 2013 at 15:46 petebowdenpetebowden 9102 gold badges8 silver badges21 bronze badges 3
  • replace is a method of String and, as such, can only be called on strings. – nullability Commented Apr 30, 2013 at 15:50
  • Sorry, maybe I should remove the q part. That's not what I'm asking about, I realize I should get that error. – petebowden Commented Apr 30, 2013 at 15:51
  • @Rastapopulous yes, that would make the question clearer. The q part is just distracting. – Alnitak Commented Apr 30, 2013 at 16:04
Add a ment  | 

4 Answers 4

Reset to default 5

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).

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

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

Cut for brevity

In the ES5 specification for String.prototype.replace:

15.5.4.11 String.prototype.replace (searchValue, replaceValue)

...

If searchValue is not a regular expression, let searchString be ToString(searchValue) and search string for the first occurrence of searchString

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

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

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

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

Does replace not differentiate between "null" and null

This is because parameter 1 of replace is converted to String.

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

Furthermore, as replace takes RegExp objects, you can also think about how

RegExp(null).toString() === "/null/"; // true
发布评论

评论列表(0)

  1. 暂无评论