- 1
How to remove all whitespaces and line breaks in js ?
So i wanted to know if there's something to like remove all whitespaces and line breaks Example user says ______ P o s si b le ______ It will reply Possible
5 Antworten
+ 3
all consecutive 3 or 4 whitespaces (including newlines) are as simple... use regex /\s{3,4}/g
only first occurence by removing the 'g' at the end of regex...
simplest way to remove n occurrences of whitespaces is to loop without g flag:
for (let i=0; i<n; ++i) {
str = str.replace(/\s/,"");
}
by appending '+' after '\s' you target any number of consecutives whitespaces at once.
by appending '{a,b}' you target from a to b consecutives whitespaces (a and b must be int and a must be lower than b)
only '{a}' target exactly a consecutive whitespaces...
+ 3
var str = "P o s si\nb le"';
console.log(str.replace(/\s+/g,'""));
+ 2
For instance the regex /\s/g removes all white space characters
https://code.sololearn.com/c2NNbhn6yL51/?ref=app
+ 1
look into the replace function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
0
Oh thx John Doe and visph :D
So some questions, is there something like '.trim()' but removes everything except 1st index ?
Also is there a way to limit this to remove only 3 or 4 white spaces ?
.-.