JSFiddle - React, Tailwind, and code Playground

JavaScript

$(function(){

alert(
    'string t e s t string a b oops h e l l o world'.replace(/(\s|^)((?:\w\s){2,}\w)(\s|$)/gi, function(a, b, c, d) {
    return b + c.replace(/\s/g, '') + d;
})
    );
 
    
});