Reputation: 113
I need to replace "\n" characters to " " if it's standing alone.
I have a string like: "Hello\n Stack\n\n\nover\nflow"
And it should be replaced with: "Hello Stack\n\n\nover flow"
I've tried on ruby something like this:
> "Hello\n Stack\n\n\nover\nflow".gsub(/\n(?![\n])/, " ")
=> "Hello Stack\n\n over flow"
But it saves an extra "\n"
Upvotes: 0
Views: 534
Reputation: 31952
Im guessing you need something like /(?<!\n)\n(?!\n)/
. To check that there is no \n
before or after the matching one.
Thanks @JohnySkovdal for the correction
Upvotes: 3