Reputation: 363
In my <td>
tag, there are few -.
in it. What I want to do is, put <br>
tag in front of this specific word. I did replace()
function but it changed just one -.
How do I find all instances of -.
?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. -.Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. -.It has survived not only five centuries, but also the leap into electronic typesetting.
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
-. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.
-. It has survived not only five centuries, but also the leap into electronic typesetting.
<table class="Notice">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
</tr>
<thead>
<tbody>
<tr>
<td>Lorem Ipsum is simply dummy text of the printing and typesetting industry. -.Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. -.It has survived not only five centuries, but also the leap into electronic typesetting.</td>
</tr>
</tbody>
</table>
$('td:contains("-.")').html(function (i, htm) {
return htm.replace(/-./g, "<br>-.");
});
I found my mistake - I didn't do 'every' word. So I used the each()
function, and it works perfectly!
$('td:contains("-.")').each(function () {
$(this).html($(this).html().replace(/-./g, "<br>-."));
});
Upvotes: 3
Views: 2139
Reputation: 313
JavaScript
<p id="changeMe"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. -.Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. -.It has survived not only five centuries, but also the leap into electronic typesetting.</p>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0.min.js"></script> -->
<script>
var p = document.getElementById("changeMe");
p.innerHTML = p.innerHTML.replace(/-./g, "<br>-. ");
</script>
Upvotes: 0
Reputation: 2183
Try this:
<!DOCTYPE html>
<html>
<body>
<p>Click the button</p>
<p id="demo">Lorem Ipsum is simply dummy text of the printing and typesetting industry. -.Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. -.It has survived not only five centuries, but also the leap into electronic typesetting.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var text = document.getElementById("demo").innerHTML;
text = text.replace(new RegExp("-.","g"), "<br>-.");
document.getElementById("demo").innerHTML = text ;
}
</script>
</body>
</html>
Upvotes: 2