jQuery:在textarea段落中找到单词并用word替换它

时间:2016-03-06 21:31:25

标签: javascript php jquery mysql ajax

我正在尝试将文本/段落从原来的英文版本翻译成谷女孩谈话。例如,“hi”将是“hellloooooo,是什么?”。这些单词已被输入数据库,其中包含英文单词及其翻译的山谷女孩版本。示例:英文专栏 - >嗨,VG柱 - > hellloooooo,英语 - >是的,VG - >喜欢,地狱是的

我正在使用MySQL从数据库中获取翻译后的单词并返回json。

所以这就是我在AJAX中检索它的方式:

$.ajax({
    type: "GET",
    url: "dictionary.php",
    success: function(data) {
        var json = JSON.parse(data);
        // replacing all the ing's with in'
        $("#textarea").each(function(){
            var ing = $(this).val().replace(/ing/g,"in'");
            $(this).val(ing);
            // finding the English words and replacing with VG 
            $.each(json, function(idx, obj) {
                var vg= $("#content").val().replace(/obj.english/g, obj.vg);
                $("#textarea").val(vg);
            );
        });                 
    }
});

我很好地接受了“ing”替换,但是尝试更换山谷女孩的话是不行的。我是否错误地循环了json对象?

编辑:这是json数据

[{"english":"hi","0":"hi","vg":"hellloooooo!","1":"hellloooooo"},    {"english":"yes","0":"yes","vg":"like, hells yes","1":"like, hells yes"},.....]

3 个答案:

答案 0 :(得分:0)

您的代码似乎有几个逻辑错误。你是在循环使用id的jQuery选择吗?无论你做什么,id总是被视为唯一的,因此它被称为id :)然后你每次都替换html元素的值(在这种情况下恰好是一次,但这是你的错误我假设)。

答案 1 :(得分:0)

请看这个替换单词的小例子,然后将其输出到一个新字段,这可能对你想要实现的目标有所帮助。

它遍历每个单词并进行精确的单词匹配,如果某些翻译还包含英语单词,则可能会遇到问题



$(document).ready(function(){
  
 var json = [{"english":"hi","0":"hi","vg":"hellloooooo!","1":"hellloooooo"},    {"english":"yes","0":"yes","vg":"like, hells yes","1":"like, hells yes"}];

var userInput = $('#textarea').val();

json.forEach(function(word){
  console.log(word);
  userInput = userInput.replace(new RegExp("\\b"+word.english+"\\b","g"),word.vg);
});
  $('#translation').val(userInput);
});

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <textarea id="textarea">
    hi this is a test of yes translation. hi yes
  </textarea>
  <textarea id="translation">
  
  </textarea>
  </body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我稍微更改了你的html,以便你的代码能够正常工作。

<强> HTML:

<textarea class="text">
            hi this is a text area what  is going on? hi how are you? yes, I am fine.
</textarea>

<textarea class="text">
            hi this is a text area what is going on? hi how are you? yes, I am fine.
</textarea>

<强>的JavaScript / JQuery的:

$.ajax({
            url: "dictionary.php",
            type:"GET",
            success: function(data) {

            //store the parsed json
            var json = JSON.parse(data);                 


         //loop through the .text elements
        $(".text").each(function(){//begin outer each

            //reference the current object
            var that = $(this);

            //replace the all of the ing's with in'
            var ing = $(this).val().replace(/ing/g,"in'");

            $(this).val(ing);

            // finding the English words and replacing with VG 
            $.each(json, function(idx, obj) {//begin inner each

                //Create a new regular expression that matches the word only.
                var regExp = new RegExp("\\b" + obj.english,"g");

                //Replace the value of the text value with the vg value and store it.
                var vg = that.val().replace(regExp, obj.vg);

                //Replace the value of the .text text area in the dom.
                that.val(vg);


            });//<-- was missing a closing } bracket. //end inner each

        });//end outer each    


            }
});