JSFiddle - React, Tailwind, and code Playground

by Carlos Morales

HTML

<p>Search: <input type="text" onchange="return trim(this)" /></p>

<p>Search2: <input type="text" onchange="return trim(this)" /></p>

<p>Search3: <input type="text" onchange="return trim(this)" /></p>


<p>Search4: <input type="text" onchange="return trim(this)" /></p>


<p>Search5: <input type="text" onchange="return trim(this)" /></p>

JavaScript

function trim(el) {
    el.value = el.value.
    replace(/(^\s*)|(\s*$)/gi, ""). // removes leading and trailing spaces
    replace(/[ ]{2,}/gi, " "). // replaces multiple spaces with one space 
    replace(/\n +/, "\n"); // Removes spaces after newlines
    return;
}