How would you use a regular expression to join lines together?

0 Dol Lod · March 7, 2015
Let me clarify what I mean, suppose I have the following sentence, I ran over the road. and it is in broken lines like this.

ex.
Input:
I ran over
the 
road.
Output:
I ran over the road

How would I join all those lines with a regular expression. I've tried Googling and implementing suggestions ,but for one particular text file, nothing has worked at all. So I figured out what I need to do, somehow combine all lowercase word if they continue on the next line into one. It's not perfect, but it will at get the text into a workable state? How would I accomplish this joining?

Post a Reply

Replies

Oldest  Newest  Rating
0 michael michael · March 8, 2015
In JavaScript I could accomplish this task this way:

// Remove linebreaks.
var rmLinebreak     = new RegExp('\\n', 'img');
// Remove multiple blanks.
var rmMultiblanks   = new RegExp('\\s{2,}', 'img');
    
input = input
                 .trim() // Remove blanks at begin. & end.
                    .replace(rmLinebreak, '')
                        .replace(rmMultiblanks, ' ');
  • 1

General Chat

107,326 followers
About

Chat about anything non-computer related including, music, movies, tuna sandwiches, and more!

Links
Moderators
Bucky Roberts Administrator