Whenever someone answers a regex question they (of course) give the correct answer, but there is usually never an explanation of what is happening. Sometimes I think the answer is very clever, but unless you know regex already you won't know that.
Someone just gave this for this question:
str.replace(/( )|[ \t\n]/g, '')
...as an answer, and it works specifically for what the questioner wanted, so it makes it completely limited to one exact thing?
So my question is... can everyone include some form of explanation of what the regex is doing piece by piece? (You can still answer it quick then go back and expand on it later.)
\s
represents any kind of whitespace including\t
and\n
.\h
is the proper replacement for[ \t\n]
.