I want to replace all occurrences of white space characters (space, tab, newline) in JavaScript.
How to do so?
I tried:
str.replace(/ /gi, \"X\
We can also use this if we want to change all multiple joined blank spaces with a single character:
str.replace(/\s+/g,'X');
See it in action here: https://regex101.com/r/d9d53G/1
Explanation
/
\s+/ g
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)