jQuery一次性替换字符串中的多个单词

时间:2017-09-07 04:14:34

标签: jquery replace

我想做的事

我想输出JAN17FEB18MAR19但输出JAN19FEB17MAR18

你能帮助我,告诉我它为什么会发生吗?



$('.policyCopyBtn').click(function() {

  var leftContent = $(".leftPart").html().replace("16</dateto>", "17</dateto>").replace("17</dateto>", "18</dateto>").replace("18</dateto>", "19</dateto>");
  var leftContent2 = $(leftContent).text();
  $('.rightPart textarea').val(leftContent2);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div class="leftPart">
  <pre>
<DateTo> Some text here JAN16</DateTo>
<DateTo> Some text here FEB17</DateTo>
<DateTo> Some text here MAR18</DateTo>
<DateTo> Some text here JAN16</DateTo>
<DateTo> Some text here FEB17</DateTo>
<DateTo> Some text here MAR18</DateTo>
</pre></div>
<div class="rightPart">
  <textarea class="policyDetails"></textarea>
</div>
<input type="submit" class="policyCopyBtn" />
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

这是因为您正在链接.replace操作,以便他们替换自己的结果。

最简单的选择是颠倒替换操作的顺序。

或者,您可以将它们全部合并为一个操作,并使用带有capture replacement function的正则表达式。

&#13;
&#13;
$('.policyCopyBtn').click(function() {
  var leftContent = $(".leftPart").html().replace(/(16|17|18)\<\/dateto\>/g, 
    function (match, capture) {
      return (Number(capture) + 1) + "</dateto>";
    }
  );
  var leftContent2 = $(leftContent).text();
  $('.rightPart textarea').val(leftContent2);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="leftPart">
  <pre>
<DateTo> Some text here JAN16</DateTo>
<DateTo> Some text here FEB17</DateTo>
<DateTo> Some text here MAR18</DateTo>
</pre></div>
<div class="rightPart">
  <textarea class="policyDetails"></textarea>
</div>
<input type="submit" class="policyCopyBtn" />
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这是更新的小提琴。

<div class="leftPart">
  <pre>
<DateTo> Some text here JAN16</DateTo>
<DateTo> Some text here FEB17</DateTo>
<DateTo> Some text here MAR18</DateTo>
</pre></div>
<div class="rightPart">
  <textarea class="policyDetails"></textarea>
</div>
<input type="submit" class="policyCopyBtn" />



$('.policyCopyBtn').click(function() {
  var leftContent = $(".leftPart").html().replace("18</dateto>", "19</dateto>").replace("17</dateto>", "18</dateto>").replace("16</dateto>", "17</dateto>");

  var leftContent2 = $(leftContent).text();
  $('.rightPart textarea').val(leftContent2);
});

FIDDLE

答案 2 :(得分:1)

您可以使用replace()回调并一次性替换所有内容

$('.policyCopyBtn').click(function() {

  var left = $(".leftPart").html().replace(/(16|17|18|19)\<\/dateto\>/g, function(r) {
    return ((+r.replace(/\D+/g,'')) + 1) + '</dateto>';
  });
  
  var left2 = $(left).text();
  $('.rightPart textarea').val(left2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div class="leftPart">
  <pre>
<DateTo> Some text here JAN16</DateTo>
<DateTo> Some text here FEB17</DateTo>
<DateTo> Some text here MAR18</DateTo>
</pre></div>
<div class="rightPart">
  <textarea class="policyDetails"></textarea>
</div>
<input type="submit" class="policyCopyBtn" />

如果你想只增加任何日期,那就是

.replace(/(\d+)\<\/dateto\>/g,  ...

FIDDLE

相关问题