<!DOCTYPE html>
<html>
<body>

<p>Click the button to alert the string with removed whitespace.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myTrim(x) {
    return x.replace(/^\s+|\s+$/gm,'');
}

function myFunction() {
    var str = myTrim("        Hello World!        ");
    alert(str);
}
</script>

</body>
</html>