Skip to content

[Copy plain Text] Regex replace help #549

Closed Answered by erosman
rosgr100 asked this question in Q&A
Discussion options

You must be logged in to vote

Basic

Here is a JavaScript replace() example:

const str = `Text
Second line text`;
const newStr = str.replace(/\r?\n/, ' ');
// Text Second line text

If you want more than one line to be replaced, then:

const newStr = str.replace(/\r?\n/g, ' ');

In Copy PlainText:

Find: /\r?\n/
Replace: put a single space

Advanced

There are more possible variations which might require adjustment to the regex e.g.

Text


Second line text

Or ...

Text (space)(space)(space)
Second line text

This is a more comprehensive regx:

const str = `Text
Second line text`;
const newStr = str.replace(/\s*[\r\n]+\s*/g, ' ');

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by rosgr100
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants