How can I replace certain carriage return line feed followed by a dash with a <br/>?

好久不见. 提交于 2019-12-06 01:48:23

Your question says you only care about DISPLAYING them on separate lines, not specifically about replacing the \r\n.

You could consider css to solve this problem.

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

See how the different white-space values work.

This works for your specific case:

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

Instead of looking for the CRLF characters, it takes any white space followed by a dash. If there are dashes in your content that follow spaces, they will also have the br tag added.

There is some strange behavior with IE 8 for newlines. If I get an explanation I'll update this.

You're only replacing \n-, but you stated that the character sequence you're trying to replace is \r\n-

Try putting the \r into your replace expressions.

\r characters, while not a normally supported windows/linux newline, will often still show as a newline in browsers/source viewers which try to cater to everyone.

You could try

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

which replaces both newline and carriage return characters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!