$1 must be inside the string:
"string".replace(/st(ring)/, "gold $1")
// output -> "gold ring"
with a function:
"string".replace(/st(ring)/, function (match, capture) {
return "gold " + capture + "|" + match;
});
// output -> "gold ring|string"
Answer from Joe on Stack Overflow$1 must be inside the string:
"string".replace(/st(ring)/, "gold $1")
// output -> "gold ring"
with a function:
"string".replace(/st(ring)/, function (match, capture) {
return "gold " + capture + "|" + match;
});
// output -> "gold ring|string"
I think you're looking for new RegExp(pattern, modifiers).
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
JavaScript String Replace with regular expression and function as arguments - Stack Overflow
JavaScript replace method takes functions as arguments - JavaScript - SitePoint Forums | Web Development & Design Community
regex - Javascript: how to pass found string.replace value to function? - Stack Overflow
How does the replace function of a string work in JavaScript when passing a function as its second argument - Stack Overflow
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
replaceValueis a function, then for each matched substring, call the function with the following m + 3 arguments. Argument 1 is the substring that matched. IfsearchValueis 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 isstring.
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
The ECMAScript spec has:
String.prototype.replace (searchValue, replaceValue)
If searchValue is a regular expression (an object whose [[Class]] property is "RegExp"), do the following: If searchValue. global is false, then search string for the first match of the regular expression searchValue. If searchValue.global is true, then search string for all matches of the regular expression searchValue. Do the search in the same manner as in String.prototype.match, including the update of searchValue. lastIndex. Let m be the number of left capturing parentheses in searchValue (NCapturingParens as specified in 15.10.2.1).
If searchValue is not a regular expression, let searchString be ToString(searchValue) and search string for the first occurrence of searchString. Let m be 0.
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. The result is a string value derived from the original input by replacing each matched substring with the corresponding return value of the function call, converted to a string if need be.
It's a bit confusing but I think Zakas is right.
When you use string.replace(rx,function) then the function is called with the following arguments:
- The matched substring
- Match1,2,3,4 etc (parenthesized substring matches)
- The offset of the substring
- The full string
You can read all about it here
In your case $1 equals Match1, so you can rewrite your code to the following and it should work as you desire:
var str = "0123";
var i = 0;
str.replace(/(\d)/g,function(s,m1){i++;return m1;});
alert(i);
The expression
function(s){i++;return s;}('$1')
Creates the function and immediately evaluates it, passing $1 as an argument. The str.replace method already receives a string as its second argument, not a function. I believe you want this:
str.replace(/(\d)/g,function(s){i++;return s;});