DEV Community

Discussion on: 1 line of code: How to convert tabs to spaces

Collapse
 
lexlohr profile image
Alex Lohr

There's an edge case inherent to tabs this one-liner misses: tabs mask previous spaces within their length, so \s\t should become \t, unless tab length was one.

However, if there are no stray spaces hidden beneath the tabs, it should work.

Collapse
 
martinkr profile image
Martin Krause

Hey Alex,

Thank you, good catch! You want to contribute an optimised version and I update the article?

Cheers!

Collapse
 
lexlohr profile image
Alex Lohr • Edited

Unfortunately, that's not a one-liner anymore, as you would have to calculate the expected position of the tab even inside the string, so it's a lot more complex. I would advise to merely document this edge case.