JSFiddle - React, Tailwind, and code Playground
HTML
" Text is text "
JavaScript
var text = " Text is text ".
var res = text.replace(/(^(\s+)|(\s+)$)/g,function(spaces){ return spaces.replace(/\s/g,"");});
" Text is text "
var text = " Text is text ".
var res = text.replace(/(^(\s+)|(\s+)$)/g,function(spaces){ return spaces.replace(/\s/g,"");});