exAspArk
exAspArk

Reputation: 113

Regex: replace single characters

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

Answers (1)

Karthik T
Karthik T

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

Related Questions