Call ajax function in replace ()

I have a function containing an ajax call:

function example(param, callback) { $.ajax({ type: "GET", url: param, contentType: "application/json; charset=utf-8", dataType: "jsonp", success: function(data) { // do something with data callback(data); } }); } 

I call it:

 example("http://www.example.com", function(result) { // do something with result }) 

But , I would like to use example() in this context:

 text.replace(/[regex not shown]/g, function(){ return RegExp.$1 + example(RegExp.$2); // does not work }); 

that is, the regular expression finds a few matches, to which I add example([whatever it matched]) . Is there any way to integrate

 example("http://www.example.com", function(result) { // do something with result }) 

in text.replace() ?

Thank you in advance!

+7
source share
4 answers

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) { // do something with data o[p]=t.toString().replace(re, function(m){ if(m==RegExp.$1){return v[1];}; if(m==RegExp.$2){return data.toString();}; }); } }); }; 

And name it as follows:

 text.myReplace(/[regex not shown]/g, this/* or whatever object is */, 'text'}); 
+1
source

Create a function to call ajax and handle the replacement of matches on the regular expression. Based on what you indicated above, this is the most modular approach, assuming that you want to perform these types of substitutions more than once.

 function replaceTextAfterAjax(str, regex, matchSendToServer, fn) { var matches = regex.exec(str); var externUrl = matches[matchSendToServer]; $.ajax({ type: "GET", url: externUrl, contentType: "application/json; charset=utf-8", dataType: "jsonp", success: function(json) { fn(json.serverSideReplaceText, matches); }, }) } var text = "go to http://www.example.com"; replaceTextAfterAjax(text, /(go to) (.*)$/, 2, function(response, matches) { text = matches[1] + ' ' + response; // continue to use `text` }); 

Note that you must support the use of RegExp local by invoking exec on the regex instance. This protects your code from threads and prevents other methods from receiving another RegExp call. The value of $ N.

0
source

friend, I don’t understand your question clearly ... you can try this code below ... not sure if this will work or not ...

 function example(param, callback) { $.ajax({ type: "GET", url: param, contentType: "application/json; charset=utf-8", dataType: "jsonp", success: function(data) { // do something with data if(typeof callback!='undefined'){callback(data)}else{return data}; } }); } 
0
source

Try deploying JS:

https://github.com/rhyneandrew/Conversation.JS

It allows you to make function calls in a completely new way, essentially associating calls with events, rather than making explicit calls. This will allow you to do exactly what you are trying to do in a much more “untied” manner, which means that in the future it will be easy to change and save! Remember that nested dependencies are never good!

0
source

All Articles