Javascript 替换html文本区域中的文本

Javascript 替换html文本区域中的文本,javascript,jquery,regex,Javascript,Jquery,Regex,我有一个脚本,它匹配一个文本区域中键入的文本,如果在减号之间 $('#notes').keyup(function() { // notes is the id of the textarea var regex = /\-([^\)]+)\-/gmi; // it gets the text between minus sign. Example: -text- var myValue = $(this).val(); if(myValu

我有一个脚本,它匹配一个文本区域中键入的文本,如果在减号之间

    $('#notes').keyup(function() { // notes is the id of the textarea
        var regex = /\-([^\)]+)\-/gmi; // it gets the text between minus sign. Example: -text-
        var myValue = $(this).val();
        if(myValue.match(regex)){
            var reference = myValue.match(regex);
            $.ajax({
                async: false, 
                type: "POST",
                url: "scripts/script.php",
                data: { "reference" : reference },
                success: function(data) {   
                    // I need to replace the text matched by the regex with data in the textarea.
                    // I need to stop the ajax calling after success or call it only if regex is matched
                }
            }); 
        }
    });
当文本与正则表达式匹配时,它向在数据库中搜索世界并返回定义的脚本发送一个ajax post调用。我需要用数据替换正则表达式匹配的文本,即数据库提取的定义

此外,我只想在匹配regex的情况下启动ajaxpost调用。这只是第一次起作用。在第一次匹配之后,它仍然会为每个键向发送调用。

请尝试以下代码

var triggerTime;
$("#notes").keyup(function() { // notes is the id of the textarea
        clearTimeout(triggerTime);
        var myValue = $(this).val();
        triggerTime = setTimeout(function(){
            var regex = /\-([^\)]+)\-/gmi; // it gets the text between minus sign. Example: -text-
            if(myValue.match(regex)){
                var reference = myValue.match(regex);
                $.ajax({
                    async: false, 
                    type: "POST",
                    url: "scripts/script.php",
                    data: { "reference" : reference },
                    success: function(data) {   
                        // I need to replace the text matched by the regex with data in the textarea.
                        // I need to stop the ajax calling after success or call it only if regex is matched
                    }
                }); 
            }
        }, 3000);// Call request in 3 second
    });
这是代码的优化版本。它将等待用户完成,在3秒钟的不活动时间内,它将生成一个Ajax调用


您可以将频率更改为2000或1000(分别为2秒和1秒)。

我解决了将contenteditable=“true”添加到文本区域的问题。 下面是最终的jquery代码:

    var textInput = $("#notes"); // the ID of the textarea
    var triggerTime;
    $("#notes").keyup(function() { // notes is the id of the textarea
        clearTimeout(triggerTime);
        var myValue = $(this).text();
        triggerTime = setTimeout(function(){
            var regex = /\-([^\)]+)\-/gmi; // it gets the text between minus sign. Example: -text-
            if(myValue.match(regex)){
                var newValue;
                var reference = myValue.match(regex);
                $.ajax({
                    async: false, 
                    type: "POST",
                    url: "scripts/parser.php",
                    data: { "reference" : reference },
                    success: function(data) {       
                        newValue = data;    
                        console.log(newValue);
                        $('.textarea').html(function() {
                            return $(this).text().replace(reference[0], '<strong>' + newValue + '</strong>');
                        });
                    }
                }); 
            }
        }, 1000);// Call request in 1 second
    });
var textInput=$(“#注释”);//文本区域的ID
var触发时间;
$(“#notes”).keyup(function(){//notes是textarea的id
clearTimeout(触发时间);
var myValue=$(this.text();
triggerTime=setTimeout(函数(){
var regex=/\-([^\)]+)\-/gmi;//它获取减号之间的文本。示例:-text-
if(myValue.match(regex)){
var新值;
var reference=myValue.match(regex);
$.ajax({
async:false,
类型:“POST”,
url:“scripts/parser.php”,
数据:{“参考”:参考},
成功:函数(数据){
newValue=数据;
console.log(newValue);
$('.textarea').html(函数(){
返回$(this).text().replace(引用[0],''+newValue+'');
});
}
}); 
}
},1000);//1秒内调用请求
});

您想替换用户在输入时输入的内容吗?我建议考虑其他方法,这听起来像是非常糟糕的UX…而不是在Enter上使用它不是一个iput,它是一个文本区域,它不是在键入时,而只是减号之间的文本。所以调用replace就像你对match所做的那样?我尝试过这个方法,但没有错误,文本区域中没有更改值:$((参考)[0])。replaceWith($(参考)[0],newValue);//其中newValue is=dataIt在匹配后键入时仍在进行进一步调用。是否要在第一次调用后停止调用?是。我希望调用正好在正则表达式匹配时发生。当前,它应该调用匹配,但在替换为新文本后,是否要替换/删除这些“-”(减号)?php脚本返回一个短语,该短语应替换-text-。但它不起作用,所以可能是因为这个原因,它一直在打电话。有没有关于如何替换-text-的建议?