When I use regular expression to replace white spaces, \s is also matching the end of a line and not just the space in a line. How do I rectify this to just replace the whitespaces in a line.
Don’t match whitespace at the end of line
notepadregex
notepadregex
When I use regular expression to replace white spaces, \s is also matching the end of a line and not just the space in a line. How do I rectify this to just replace the whitespaces in a line.
Best Answer
\s
stands for any kind of white space, ie. simple space, tabulation, line break...You can use
\h
for horizontal space: space or tabulation.If you want to match only spaces between words, you can use:
(?<=\S)\h+(?=\S)
where
(?<=\S)
is a positive lookbehind that make sure we have a non whitespace before(?=\S)
is a positive lookahead that make sure we have a non whitespace after.