How to remove all whitespace from string in JQuery?

By Hardik Savani | May 24, 2016 | Category : Javascript HTML jQuery

Sometimes we require to remove white space from given string. So at that time you can delete spaces from string using replace() of core php. replace() will help to replace your blank space or empty space or extra space or trailing space from string that way we can remove. you can do it from bellow example.


<html lang="en">


<title>Jquery - Remove white space from string</title>

<script src=""></script>



<div class="mytext">

This is my site.


<script type="text/javascript">

var myText = $(".mytext").text();

var newMyText = myText.replace(/ /g,'');





Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.