How to Remove All Spaces from an Array in JavaScript
We can use regular expression /\s/g to remove all spaces in JavaScript, but the problem is that it will also remove all line breaks when we want to keep the line breaks. The solution is that we can use /\x20/g instead of /\s/g wheer \s is space and \x20 is line break in JavaScript.
Original String
<textarea oninput = "show_1.innerText = this.value"></textarea> <div id = "show_1"></div>
String with All Spaces Removed where Line Breaks are Also Removed
We can remove all sapces from a sring using regular expression /\s/g. But the problem is we will also remove all
line breaks too.
<textarea oninput="show_2.innerText = this.value.replace(/\s/g,'')"></textarea> <div id="show_2"></div>
String with All Line Breaks Removed
<textarea oninput="show_3.innerText = this.value.replace(/\n/g,'')"></textarea> <div id="show_3"></div>
String with Just Spaces Removed where Line Breaks are Preserved
\x20 means a space in JavaScript.
<textarea oninput="show_4.innerText = this.value.replace(/\x20/g,'')"></textarea> <div id="show_4"></div>
Comments
Post a Comment