title: JS - How to remove all whitespaces except in the middle of the string, that is generated using substr() function?
body: <p>I'm creating a JS / JQuery calculator and there is an <code>#info</code> element, which shows the user what they typed, or what is the calculation sequence like in the "Windows 10 Calculator", here:
  <a href="https://i.stack.imgur.com/pX9Df.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pX9Df.png" alt="enter image description here"></a></p>
  
  <p>I've tried many things which I'll list below but they didn't work.</p>
  
  <hr>
  
  <p>Let me show you how it should look like:</p>
  
  <ol>
  <li>The user just pressed the "Sqrt" button. The <code>#info</code> element has changed accordingly. By using "Inspect element", we can see, <code>#info</code> element has one whitespace from each side: <code>" √(0) "</code>.
  <a href="https://i.stack.imgur.com/vi3KX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vi3KX.png" alt="enter image description here"></a></li>
  <li><p>Let's press the button one more time and see what we've got. Now, the <code>#info</code> element has more whitespaces: <code>" √( √(0)  )"</code>. One from the left side, one after the first <code>√(</code>, and two whitespaces after the <code>√(0)</code>, until we reach <code>)</code>. This behavior is caused by <code>substr()</code> function.</p>
  
  <p><strong>However, it should be only one whitespace each side</strong> <code>√(0)</code> string. Without any other whitespaces. <code>substr()</code> function <strong>copies my whitespaces</strong> and that's the wrong thing.</p>
  
  <p>So, above, using <code>" √( √(0)  )"</code> example, it should be <code>"√( √(0) )"</code>, and by pressing "Sqrt" button again, we should get the result of <code>#info</code> as <code>"√(√( √(0) ))"</code>.
  <a href="https://i.stack.imgur.com/1dt04.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1dt04.png" alt="enter image description here"></a></p></li>
  </ol>
  
  <hr>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let firstResult, firstResultWithSign;
  
  $("#sqrt").on("click", function() {
    if (typeof firstResult === "undefined") {
      firstResult = $("#result h3").text();
    }
  
    if (typeof firstResultWithSign === 'undefined') {
      firstResultWithSign = ' \u221A(' + firstResult + ') ';
    }
  
    $("#result h3").text(Math.sqrt($("#result h3").text()));
  
    if (
      $("#info")
      .text()
      .lastIndexOf("\u221A") &gt; -1
    ) {
      /**************************************************************************************/
      $('#info').text($('#info').text().substr(0, $('#info').text().lastIndexOf(firstResultWithSign) + 3) + firstResultWithSign + $('#info').text().substr($('#info').text().indexOf(')') + 1) + ')');
      /**************************************************************************************/
    } else {
      $("#info").html(firstResultWithSign);
    }
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    margin: 0;
    padding: 0;
    font-family: sans-serif;
    background-image: url("https://images8.alphacoders.com/702/702959.jpg");
    background-attachment: fixed;
    background-size: cover;
  }
  
  * {
    box-sizing: border-box;
    -webkit-user-select: none;
    user-select: none;
  }
  
  .text-center {
    text-align: center;
  }
  
  #result {
    background-color: #eee;
    min-height: 150px;
    max-height: 200px;
    position: relative;
  }
  
  #result h3 {
    font-size: 40px;
    margin: 0;
    position: absolute;
    bottom: 15px;
    right: 15px;
    -webkit-user-select: text;
    user-select: text;
  }
  
  .history {
    text-align: right;
    padding: 17px 17px 10px;
    display: inline-block;
    float: right;
  }
  
  .history:hover {
    background-color: #ddd;
  }
  
  #main-panel {
    background-color: #eee;
  }
  
  #title {
    float: left;
    padding: 17px;
  }
  
  #info {
    position: absolute;
    right: 15px;
    top: 55px;
    color: #616161;
    font-size: 15px;
  }
  
  .column {
    float: left;
    width: calc(25% - 5px);
    background-color: #ddd;
    height: 40px;
    margin: 0 4px 4px 0;
    line-height: 40px;
  }
  
  #one,
  #two,
  #three,
  #four,
  #five,
  #six,
  #seven,
  #eight,
  #nine,
  #zero {
    background-color: #fff;
    font-weight: bold;
  }
  
  #divide:hover,
  #times:hover,
  #minus:hover,
  #plus:hover,
  #equals:hover {
    background-color: #01579b !important;
    color: #fff;
  }
  
  #divide:active,
  #times:active,
  #minus:active,
  #plus:active,
  #equals:active {
    background-color: #005395 !important;
    color: #fff;
  }
  
  .material-icons {
    font-size: 22px;
  }
  
  .column:first-child {
    margin-left: 4px;
  }
  
  .column:hover {
    background-color: #bcbcbc !important;
  }
  
  .column:active {
    background-color: #aeaeae !important;
  }
  
  .row:after {
    content: "";
    display: table;
    clear: both;
  }
  
  .fa-erase-left:before {
    content: "⌫";
  }
  
  .three-dots {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  .overflow-x-auto {
    overflow-x: auto;
  }
  
  :focus {
    outline: 0;
  }
  
  footer {
    font-size: 0.85rem;
    margin: 1rem 0;
  }
  
  a {
    text-decoration: none;
    color: #fff;
    position: relative;
    cursor: pointer;
  }
  
  footer a:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 0.0625rem;
    bottom: 0;
    left: 0;
    background-color: #fff;
    visibility: hidden;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-transition: all 0.3s ease-in-out 0s;
    transition: all 0.3s ease-in-out 0s;
  }
  
  footer a:hover:before {
    visibility: visible;
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
  }
  
  .calculator-container {
    width: 45%;
    min-width: 200px;
    margin: 2.5rem auto 0;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;html&gt;
  &lt;div class="calculator-container text-center"&gt;
    &lt;div id="result"&gt;
      &lt;div id="title"&gt;Calculator&lt;/div&gt;
      &lt;div class="history" title="History"&gt;&lt;i class="material-icons"&gt;&amp;#xE889;&lt;/i&gt;&lt;/div&gt;
      &lt;div id="info"&gt;&lt;/div&gt;
      &lt;h3 data-auto-generated="true" data-true-zero="true"&gt;0&lt;/h3&gt;
    &lt;/div&gt;
    &lt;div id="main-panel"&gt;
      &lt;div class="row"&gt;
        &lt;div id="percentage" class="column"&gt;%&lt;/div&gt;
        &lt;div id="sqrt" class="column"&gt;&amp;#x0221A;&lt;/div&gt;
        &lt;div id="exponentiation-by-two" class="column"&gt;x&lt;sup&gt;2&lt;/sup&gt;&lt;/div&gt;
        &lt;div id="one-divide-by-x" class="column"&gt;&lt;sup&gt;1&lt;/sup&gt;/x&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
        &lt;div id="ce" class="column"&gt;CE&lt;/div&gt;
        &lt;div id="c" class="column"&gt;C&lt;/div&gt;
        &lt;div id="erase-left" class="column"&gt;
          &lt;div class="fa-erase-left"&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;div id="divide" class="column"&gt;&amp;divide;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
        &lt;div id="seven" class="column"&gt;7&lt;/div&gt;
        &lt;div id="eight" class="column"&gt;8&lt;/div&gt;
        &lt;div id="nine" class="column"&gt;9&lt;/div&gt;
        &lt;div id="times" class="column"&gt;&amp;#215;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
        &lt;div id="four" class="column"&gt;4&lt;/div&gt;
        &lt;div id="five" class="column"&gt;5&lt;/div&gt;
        &lt;div id="six" class="column"&gt;6&lt;/div&gt;
        &lt;div id="minus" class="column"&gt;&amp;#8722;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
        &lt;div id="one" class="column"&gt;1&lt;/div&gt;
        &lt;div id="two" class="column"&gt;2&lt;/div&gt;
        &lt;div id="three" class="column"&gt;3&lt;/div&gt;
        &lt;div id="plus" class="column"&gt;&amp;#43;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
        &lt;div id="plus-minus" class="column"&gt;&amp;#177;&lt;/div&gt;
        &lt;div id="zero" class="column"&gt;0&lt;/div&gt;
        &lt;div id="comma" class="column"&gt;,&lt;/div&gt;
        &lt;div id="equals" class="column"&gt;&amp;#61;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;footer&gt;
      &lt;a href="https://codepen.io/Kestis500"&gt;Created by LukasLSC&lt;/a&gt;
    &lt;/footer&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>JsFiddle: <a href="https://jsfiddle.net/xob6Luhh/" rel="nofollow noreferrer">https://jsfiddle.net/xob6Luhh/</a>.</p>
  
  <hr>
  
  <p>What didn't work?</p>
  
  <ol>
  <li>Using <code>replace()</code>, because then the <code>substr()</code> will copy absolutely no whitespace.</li>
  <li>Using <code>indexOf(firstResultWithSign)</code>.</li>
  <li>Using <code>' ' + firstResultWithSignNoSpace + ' '</code>, this will make <code>substr()</code> to copy whitespaces.</li>
  </ol>
  
  <p>I've tried almost 10 ways, however, they are very similar to 1 - 3.</p>
  
  <p>Any ideas? :) Thank you.</p>