Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果它是单独存在的,我需要将“\n”字符替换为“”。
我有一个字符串,如: “Hello\n Stack\n\n\nover\nflow”
它应该替换为: “Hello Stack\n\n\nover flow”
我试过像这样的红宝石:
> "Hello\n Stack\n\n\nover\nflow".gsub(/\n(?![\n])/, " ") => "Hello Stack\n\n over flow"
但它节省了一个额外的“\n”
我猜你需要类似的东西/(?<!\n)\n(?!\n)/。\n检查匹配之前或之后是否没有。
/(?<!\n)\n(?!\n)/
\n
感谢@JohnySkovdal 的更正