Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jqueryfind&;替换输入文本中文本区域中的字符_Jquery_Regex_Replace_Find - Fatal编程技术网

jqueryfind&;替换输入文本中文本区域中的字符

jqueryfind&;替换输入文本中文本区域中的字符,jquery,regex,replace,find,Jquery,Regex,Replace,Find,我做错了什么 我的HTML <form name="frm"> <textarea rows="10" id="uinput">some text here</textarea> find 1: <input type="text" id="findbox1" value="e"> replace 1: <input type="text" id="replacebox1" value="666"> find 2

我做错了什么

我的HTML

<form name="frm">
   <textarea rows="10" id="uinput">some text here</textarea>
   find 1: <input type="text" id="findbox1" value="e">
   replace 1: <input type="text" id="replacebox1" value="666">
   find 2: <input type="text" id="findbox2" value="o">
   replace 2: <input type="text" id="replacebox2" value="000">
   find 3: <input type="text" id="findbox3" value="t">
   replace 3: <input type="text" id="replacebox3" value="777">
   <button type="button" id="fnrbtn">find and replace</button>
</form>

这里有一些文字
查找1:
替换1:
查找2:
替换2:
查找3:
替换3:
查找并替换

我的JQuery

RegExp.escape = function(str) {
  return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
};

$("#fnrbtn").click(function() {
  var InputBox = $("#uInput").val(),
    FindWhat = $('#findbox' + i).val(),
    ReplaceWhat = $('#replacebox' + i).val(),
    NewInputBox = '',
    msg = '';
  for (i = 1; i < counter; i++) {
    msg += "\n Textbox #" + i + ": " + $('#findbox' + i).val() + " Replace #" + i + ": " + $('#replacebox' + i).val();
    NewInputBox = InputBox.replace(new RegExp(RegExp.escape(FindWhat), "gi"), ReplaceWhat);
  }
  alert(msg);
  $("#uInput").val(NewInputBox);
  $('#uInput').select();
});
RegExp.escape=函数(str){
返回字符串(str).replace(/([.*+?^=!:${}()|[\]\/\]])/g,“\\$1”);
};
$(“#fnrbtn”)。单击(函数(){
var InputBox=$(“#uInput”).val(),
FindWhat=$('#findbox'+i).val(),
ReplaceWhat=$('#replacebox'+i).val(),
NewInputBox=“”,
味精='';
对于(i=1;i
您的
查找哪些内容
替换哪些内容
位于
for
循环的
之外,而我认为它们必须位于循环的内部

此外,
计数器
未定义。您需要在使用前对其进行初始化

HTML包含
$(“#uinput”)
,而不是JQuery代码中的
$(“#uinput”)

此外,您可以使用旧值
InputBox
,而不是上一次搜索和替换操作中修改的值来运行替换

下面是一个固定代码:

RegExp.escape=function(str){返回字符串(str).replace(/([.+?^=!:${}()\[\]\/\]])/g,'\\$1');
$(“#fnrbtn”)。单击(函数(){
var InputBox=$(“#uinput”).val();//uinput>uinput
味精='';
计数器=4;

我很好奇,因为我刚刚注意到你删除的答案。