How can i convert/replace every newline to '
'?

前端 未结 5 1644
南笙
南笙 2021-01-21 05:55
set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on

I tried this, content.gsub(\"\\r\\n\",\"
\")
bu

相关标签:
5条回答
  • 2021-01-21 06:11

    With Ruby On Rails 4.0.1 comes the simple_format from TextHelper. It will handle more tags than the OP requested, but will filter malicious tags from the content (sanitize).

    simple_format(t.content)
    

    Reference : http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html

    0 讨论(0)
  • 2021-01-21 06:15

    An alternative to convert every new lines to html tags <br> would be to use css to display the content as it was given :

    .wrapped-text {
        white-space: pre-wrap;
    }
    

    This will wrap the content on a new line, without altering its current form.

    0 讨论(0)
  • 2021-01-21 06:18

    You need to use html_safe if you want to render embedded HTML:

    <%= @the_string.html_safe %>
    

    If it might be nil, raw(@the_string) won't throw an exception. I'm a bit ambivalent about raw; I almost never try to display a string that might be nil.

    0 讨论(0)
  • 2021-01-21 06:21

    I'm not sure I exactly follow the question - are you seeing the output as e.g. preformatted text, or does the source HTML have those tags? If the source HTML has those tags, they should appear on new lines, even if they aren't on line breaks in the source, right?

    Anyway, I'm guessing you're dealing with automatic string escaping. Check out this other Stack Overflow question Also, this: Katz talking about this feature

    0 讨论(0)
  • 2021-01-21 06:23

    http://www.ruby-doc.org/core-1.9.3/String.html as it says there gsub expects regex and replacement since "\n\r" is a string you can see in the docs:

    if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g. '\d' will match a backlash followed by ‘d’, instead of a digit.

    so you are trying to match "\n\r", you probably want a character class containing \n or \r -[\n\r]

    a = <<-EOL
    set tabstop=4
    set shiftwidth=4
    set nu
    set ai
    syntax on
    filetype plugin indent on
    EOL
    print a.gsub(/[\n\r]/,"<br/>\n");
    
    0 讨论(0)
提交回复
热议问题