You can not. This is because you are passing the .replace () method to a function literal, so the replace method will take this .toString () function, the returned string (its source code) as an argument.
This is because the .replace () method is synchronous and does not expect a callback as the second argument, but as a string, so it converts any second argument to a string if it is not.
And if you really call the function in the parameter, then your function does not have a specific return value, it will parse "undefined" as the second value.
But you can write your own asynchronous replacement method and add it to the String prototype. I can’t edit the code on my phone, so when I get back to my computer, I will write it for you if you do not understand.
Edit:
Actually, I was wrong, you can use the callback in the substitution method. The problem is how you use the asynchronous call internally. I don’t know what exactly you are trying to do, so I hope this helps you.
String.prototype.myReplace=function(re, o, p){ var v=[]; var t=this; t.toString().replace(re, function(m){ if(m==RegExp.$1){v[1]=m;}; if(m==RegExp.$2){v[2]=m;}; }); $.ajax({ type: "GET", url: v[2], contentType: "application/json; charset=utf-8", dataType: "jsonp", success: function(data) {
And name it as follows:
text.myReplace(/[regex not shown]/g, this, 'text'});
Juan garcia
source share