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

前端 未结 5 1654
南笙
南笙 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: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]/,"
    \n");

提交回复
热议问题