3

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 -.?

Original Text

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.

What I want

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.

This is my Code example

<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>

Javascript

$('td:contains("-.")').html(function (i, htm) {
    return htm.replace(/-./g, "<br>-.");
});

Solution

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>-."));
});
LoveFortyDown
  • 1,011
  • 2
  • 17
  • 37
naanace
  • 363
  • 1
  • 4
  • 17
  • 2
    possible duplicate of [Replacing all occurrences of a string in JavaScript](http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript) – kuldeep.kamboj Mar 10 '15 at 05:01
  • Duplicate of http://stackoverflow.com/a/13574989/1107638 – Mox Shah Mar 10 '15 at 05:01
  • You want to replace or you want to add in front of that?? – Guruprasad J Rao Mar 10 '15 at 05:14
  • Can you provide your javascript code where you used replace(). May be there is a mistake threre? Actually it should work as you expect. See for an examples here: http://www.w3schools.com/jsref/jsref_replace.asp – candle Mar 10 '15 at 05:15
  • @candle I add! :) This way works just one `-.` part. – naanace Mar 10 '15 at 05:25
  • @GuruprasadRao add in front of this! I used 'replace' method because I thought it change html and do
    tag.
    – naanace Mar 10 '15 at 05:30
  • Try the replaceAll function from here: http://stackoverflow.com/questions/1137436/useful-javascript-methods-that-extends-built-in-objects/1137579#1137579 – candle Mar 10 '15 at 05:37

3 Answers3

3

Use JavaScript replace() function with global matching.

replace(/-/g,"<br />-");
Amit
  • 827
  • 10
  • 17
  • Thanks :) But that function was just for one `-.` . Finally I solved myself with each() function. `$('td:contains("-.")').each(function () { $(this).html($(this).html().replace(/-./g, "
    -.")); });` this works!
    – naanace Mar 10 '15 at 05:45
2

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>
candle
  • 1,963
  • 3
  • 15
  • 24
0

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>
UserName Name
  • 267
  • 2
  • 3