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>Can anyone advise how I should do this?
, , /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>-
/\(|\)/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>