用替换函数在javascript中替换全局

时间:2016-11-08 07:22:47

标签: javascript

我想用javascript替换一些文本。 但它只工作一次。

  passageText = passageText.replace('</span><span alignmentBaseline="useDominantBaseline"', '</span><br><span alignmentBaseline="useDominantBaseline"');

1 个答案:

答案 0 :(得分:0)

$( document ).ready(function() {
var passageText = '<span>gaurav</span><span alignmentBaseline="useDominantBaseline">is testing</span><span alignmentBaseline="useDominantBaseline">is testing1</span><span alignmentBaseline="useDominantBaseline">is testing2</span>'
var toReplace = '</span><span alignmentBaseline="useDominantBaseline"'
var re = new RegExp(toReplace, 'igm')
passageText = passageText.replace(re, '</span><br><span alignmentBaseline="useDominantBaseline"');
  
  console.log(passageText)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<p>Hope this will work for you :)<p/>