-4

I want to replace some texts in javascript. But it is working only one time.

  passageText = passageText.replace('</span><span alignmentBaseline="useDominantBaseline"', '</span><br><span alignmentBaseline="useDominantBaseline"');
Shalini
  • 27
  • 3
  • please read up what the replace does and how it works, can't replace something that doesn't match the first part of the parameters. [replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) – Kevin Kloet Nov 08 '16 at 07:27
  • And read this answer for using a [*regular expression on HTML*](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). – RobG Nov 08 '16 at 07:31

1 Answers1

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/>
Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28