looking for some regex help - white space before/after new line

purbeast0

No Lifer
Sep 13, 2001
53,582
6,424
126
i am okay with some basic regex and will just look at a regex cheat sheet (http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/) but I am having problems trying to figure this out.

what i have is a multiline string like the following (note the white space before/after the terms on each line)

Code:
   this is one term      
       another term             
 only one space

then take this modified version of the above with no white before/after each term

Code:
this is one term
another term
only one space

i'm looking to write a regex that will match the first one because it finds white space before/after each new line (don't worry about the initial/trailing white space in that string, because a .trim() handles that).

i'm having trouble with this so looking for some help. anyone?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,662
4,603
75
Regular expressions are subtly different for different languages. Especially when it comes to things like newlines. Based on your cheat sheet, I'm thinking something like this:

Code:
!/[^ ]\r|\r[^ ]/s

To match any newline with a non-space next to it; then invert the result. One or both of those might need to be \n, not \r. Your OS may affect that as well.
 

purbeast0

No Lifer
Sep 13, 2001
53,582
6,424
126
thanks for the quick help, i will check that out.

this is in javascript and it will always be ran on windows machines.

i also might have found out another way to do this that does not involve regex and can be done splitting on new lines in an angular validator.

EDIT:

yeah just went with the angular validator and did a split on new lines in the string, then just do a trim and compare the length before/after and it works fine. these aren't going to be huge strings or anything so it isn't going to be a performance hit.
 
Last edited: