JSFiddle - React, Tailwind, and code Playground

HTML

<input type="text" class="singleSpace" />

JavaScript

$('.singleSpace').keyup(function() {
        var foo = $(this).val().replace(/^( *)([^ ] *)(.*)/g, function(a,b,c,d,e){
            return $.trim(c) + ' ' + d.replace(/ /g, '').slice(0,40);
        })
        
        $(this).val(foo)
    });