[removed] how to pass found string.replace value to function?

后端 未结 3 1741
-上瘾入骨i
-上瘾入骨i 2020-12-21 09:32

When I have something like this:

var str = \"0123\";
var i = 0;
str.replace(/(\\d)/g,function(s){i++;return s;}(\'$1\'));
alert(i);

Why doe

3条回答
  •  温柔的废话
    2020-12-21 10:29

    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;});
    

提交回复
热议问题