? How to replace ASCII 13 sequence ASCII 10 - (CR LF DASH or / r ...">

How can I replace a specific carriage return line and then a dash using <br/">?

How to replace ASCII 13 sequence ASCII 10 - (CR LF DASH or / r / n -) in text inside TD element on web page using javascript or jQuery? Similar questions can be found in stackoverflow and elsewhere, but the solutions listed do not work in this particular scenario. Incoming HTML is generated dynamically by a part of my former client software, which will not be modified, but it refers to a javascript file that I can change. Below is a shortened version of the page. The actual page contains from zero to twenty rows of data, some of which contain several rows. My users are in Internet Explorer 8, and none of the two lines below work. I tried just replacing the carriage return and just replacing the line. I tried this from javascript and jQuery with no visible effect.When I save a page from Internet Explorer 8 and view it in a hex editor, there are carriage return and line characters in the client. The essence of the problem is to show / n in JavaScript text.

I want to perform this replacement because I want two lines of text to be displayed on the output at any time when the sequence / r / n - exists on the page in the element.

Note that I have included two versions of the replacement: one jQuery and one JavaScript. I also do not want to do this.

   <html>
    <head>
    <title>Page Testing </title>
    <script src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('td').each(function () {
                $this = $(this);
                $this.html($this.html().replace(/\r\n\-/g, "<br/>-"));
                this.innerHTML = (this.innerHTML.replace(/\r\n\-/g, "<br/>-"));
            });
        });
    </script>
    </head>
    <body>
    <table>
    <tbody>
    <tr>
     <td>-First Content
    -Second Line of Content</td>
    <td>-How I want it to look<br/>-After the transformation</td>
    </tr>
    </tbody>
    </table>
    </body>
    </html>
+3
source share
4 answers

Your question says that you only care about displaying them on separate lines, and not just about replacing \ r \ n.

You can consider css to solve this problem.

<style>
    tr {
        white-space:pre-line;
    }
</style>

See how different values ​​of white space work.

+11
source

This works for your specific case:

$this.html($this.html().replace(/\s+\-/g, "<br /> -"));

CRLF , . , , br.

IE 8 . , .

+1

$this.html().replace(/[\n\r]+\-/g, "<br/>-")

.

0

\n-, , , , -\r\n -

\r .

\ r , Windows/linux, - /, .

0

All Articles