调用replace()中的ajax函数

Call to ajax function within replace()

本文关键字:ajax 函数 中的 replace 调用      更新时间:2023-09-26

我有一个包含ajax调用的函数:

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

我叫它:

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

但是,我想在此上下文中使用example()

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

即,正则表达式找到多个匹配项,然后我将example([whatever it matched])添加到其中。有没有办法集成

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

进入text.replace()

提前谢谢!

你不能。这是因为您将函数文本传递给.replacement()方法,所以replace方法将函数.toString()返回的字符串(其源代码)作为参数。

这是因为.replace()方法是同步的,不希望回调作为第二个参数,而是字符串,所以如果不是,它会将任何第二个自变量转换为字符串。

如果你真的调用了参数中的函数,因为你的函数没有定义返回值,就会将"undefined"解析为第二个值。

但是您可以编写自己的异步替换方法,并将其添加到String原型中。我无法编辑手机中的代码,所以当我回到电脑时,如果你还没弄清楚,我会帮你写。

编辑:

实际上我错了,你可以在replace方法中使用回调。问题是你在里面使用异步调用的方式。我不知道你到底想做什么,所以我希望这能帮助你。

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

这样称呼它:

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

创建一个函数来进行ajax调用并处理正则表达式上匹配项的替换。根据您上面提供的内容,假设您希望多次执行这些类型的替换,这是最模块化的方法。

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

请注意,您应该通过在正则表达式实例上调用exec来保持对RegExp的本地使用。这可以确保代码线程的安全,并防止其他方法获取另一个调用的RegExp$N值。

朋友,我不太明白你的问题。。。你可以试试下面的代码。。。不确定它是否有效。。。

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

尝试ConversationJS:

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

它允许您以一种全新的方式进行函数调用,本质上是将调用绑定到事件,而不是进行显式调用。它将允许你以一种更加"解耦"的方式来做你想做的事情,这意味着它在未来也很容易更改和维护!记住,嵌套的依赖关系从来都不是好的!