<p id="text"> text </p> <script> var text = document.getElementById('text'); text.innerHTML = text.innerHTML.replace(/^\s|\s$/g, '').replace(/^.|.$/g, '<span style="color:red;">$&</span>'); </script>