JavaScript String Replace with regular expression and function as arguments

后端 未结 2 437
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 14:52

I seem to be getting conflicting advice in the books I read on this functionality. I\'m wondering if someone could clarify. For example Nicholas Zakas states the function argume

2条回答
  •  暖寄归人
    2021-02-08 15:30

    Zakas is correct, the penultimate and final arguments are pos and originalText. From the ECMA-262 3rd and 5th editions (section 15.5.4.11):

    If replaceValue is a function, then for each matched substring, call the function with the following m + 3 arguments. Argument 1 is the substring that matched. If searchValue is a regular expression, the next m arguments are all of the captures in the MatchResult (see 15.10.2.1). Argument m + 2 is the offset within string where the match occurred, and argument m + 3 is string.

    Note that String.prototype.replace was new in ECMA-262 3rd edition. MSDN documentation for JScript and MDC documentation for JavaScript both conform to this specification.

    • replace Method (Windows Scripting - JScript)
    • replace - MDC

提交回复
热议问题