with ...">

JQuery replace multiple substring / text occurrences

I'm currently trying to learn a method replacein jQuery.

I have <div class="notes">with the following text

  (1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)

and would like to replace the text with certain values. For example, every time I see )(, I want it to jump to a new line ( <br/>). I tried to use jQuery replacement method to achieve this.

 $(document).ready(function() {
    var text = $('.notes').html().replace(")(", "<br/>");
    $('.notes').html(text);
  });

I noticed that at the same time he replaced only the first instance. So I tried the method replaceAll, although this did not affect the string.

Quick demo script or snippet below:

$(document).ready(function() {
    var text = $('.notes').html().replace(")(", "<br/>");
    $('.notes').html(text);
    alert(text);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
  (1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
Run codeHide result

Can anyone advise how I should do this?

+4
5

, , /g.

:

/\)\(/g

$(document).ready(function() {
    var text = $('.notes').html().replace(/\)\(/g, "<br/>");
    $('.notes').html(text);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
  (1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
Hide result
+4

.replace() - String, jQuery, RegExp.

 var text = $('.notes').html().replace(/\)\(/g, "<br/>");

g, , , .

+4

-

/\(|\)/g ( ). g . .

$(document).ready(function() {
    var text = $('.notes').text().replace(/\(|\)/g, "<br/>");
    $('.notes').html(text);
    alert(text);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notes">
  (1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 1 1)(1 1 1 0 0 0 0)(1 1 1 0 0 1 1)
</div>
Hide result
+3

regex ( ):

$(function() {
    var notes = $('.notes');
    notes.html(notes.html().split(')(').join(')<br/>('));
});
+3
$(document).ready(function() {
  $('.notes').html($('.notes').html().replace(/\)\(/g, '<br />'));
});
0

All Articles