revision:
The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.
string.trim()
Parameters: none
<;p>trim() removes whitespace from both sides of a string:<;/p> <;pre><;p id="demo1"><;/p><;/pre> <;pre><;p id="demo2"><;/p><;/pre> <;script> let text = " Hello World! "; let result = text.trim(); document.getElementById("demo1").innerHTML = text; document.getElementById("demo2").innerHTML = result; <;/script>
trim() removes whitespace from both sides of a string:
replace() removes whitespace from both sides of a string:
<;div> <;p>trim() removes whitespace from both sides of a string:<;/p> <;pre style="border:none;"><;p id="trim-1"><;/p><;/pre> <;pre style="border: none;"><;p id="trim-2"><;/p><;/pre> <;p>replace() removes whitespace from both sides of a string:<;/p> <;pre style="border: none;"><;p id="trim-3"><;/p><;/pre> <;pre style="border: none;"><;p id="trim-4"><;/p><;/pre> <;pre style="border: none;"><;p id="trim-5"><;/p><;/pre> <;/div> <;script> let text = " Hello World! "; let result = text.trim(); document.getElementById("trim-1").innerHTML = "untrimmed text : " + text; document.getElementById("trim-2").innerHTML = "trim() method : " + result; let text1 = " Hello World! "; let result2 = text1.replace(/^\s+|\s+$/gm,''); document.getElementById("trim-3").innerHTML = "untrimmed text : " + text; document.getElementById("trim-4").innerHTML = "replace() method: " + result2; <;/script>