Skip to content
Advertisement

How to replace new lines by regular expressions

How can I set any quantity of new lines with a regular expression?

JavaScript

I need to remove every new line (n), not <br/>, between two paragraphs.

Advertisement

Answer

To begin with, str_replace() (which you referenced in your original question) is used to find a literal string and replace it. preg_replace() is used to find something that matches a regular expression and replace it.

In the following code sample I use s+ to find one or more occurrences of white space (new line, tab, space…). s is whitespace, and the + modifier means one or more of the previous thing.

JavaScript

Live Example

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement