JSFiddle - React, Tailwind, and code Playground

HTML

<div id="out"></div>

JavaScript

var str = "    tushar is a good      boy     ";

str = str.replace(/\s+/g,' ' ).replace(/^\s/,'').replace(/\s$/,'');

document.getElementById("out").innerHTML = str;